简体   繁体   English

UpdateView和CreateView-代码有效还是可以改进?

[英]UpdateView and CreateView - Is the code efficient or could improved on?

For Django 1.6, This is my final code that works. 对于Django 1.6,这是我的最终代码。 The logic is, if a database object doesn't exist for the user then go to 'account_add' to add the account. 逻辑是,如果用户不存在数据库对象,则转到'account_add'添加帐户。 I didn't want to use a user_profile or modify the user object directly for specific issues, so please do not suggest those. 对于特定问题,我不想使用user_profile或直接修改用户对象,因此请不要提出这些建议。 What I would like to know is if this code is written well, or could be written better and more efficient? 我想知道的是,这段代码写得好不好,或者可以写得更好,更高效?

class AccountCreateOrModify():
    model = Employee
    form_class = AccountForm
    template_name = 'bot_data/account_modify.html'
    success_url = reverse_lazy('home')


class ViewEmployee(LoginRequiredMixin, 
        DetailView):

    model = Employee
    template_name = 'bot_data/employee_detail.html'

    def dispatch(self, request,
            *args, **kwargs):
        try:
            pk = self.request.user.pk
            queryset = self.model.objects.get(user_assigned=pk)
            return super(ViewEmployee, 
                    self).dispatch(request, 
                            *args, **kwargs)
        except Employee.DoesNotExist:
            return redirect('account_add')

    def get_object(self):
            user = self.request.user.id
            find_user = self.model.objects.get(id=user)
            return find_user

class AccountModify(LoginRequiredMixin, 
        AccountCreateOrModify,
        UpdateView):

    def dispatch(self, request,
            *args, **kwargs):
        try:
            pk = self.request.user.pk
            queryset = self.model.objects.get(user_assigned=pk)
            return super(AccountModify, 
                    self).dispatch(request, 
                            *args, **kwargs)
        except Employee.DoesNotExist:
            return redirect('account_add')

    def get_object(self, queryset=None):
        pk = self.request.user.pk
        queryset = self.model.objects.get(user_assigned=pk)
        return queryset





class AccountCreateRecord(LoginRequiredMixin,
        AccountCreateOrModify,
        CreateView):
    print "filler"

try blocks should contains a single line: try块应包含一行:

def dispatch(self, request, *args, **kwargs):
    pk = self.request.user.pk
    try:
        queryset = self.model.objects.get(user_assigned=pk)
    except Employee.DoesNotExist:
        return redirect('account_add')
    return super(ViewEmployee, self).dispatch(
        request, *args, **kwargs)

If except wasn't returning anything, look for try , except , else block succession. 如果except不返回任何内容,请查找tryexceptelse阻止继承。

classes should inherits from object or have no parenthesis: 类应该继承自object或没有括号:

class AccountCreateOrModify(object): # a bit better
    pass

class AccountCreateOrModify: # avoid useless parenthesis
    pass

If Employee is an abstract class you can avoid to hit database. 如果Employee是一个抽象类,则可以避免访问数据库。

def get_object(self):
    user = self.request.user.id
    find_user = self.model.objects.get(id=user)
    return find_user

Why "print" in your last class, did you know about the pass (no-op) keyword? 为什么在上一堂课中“打印”,您知道pass(无操作)关键字吗?

class AccountCreateRecord(LoginRequiredMixin, AccountCreateOrModify,
                          CreateView):
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CreateView不在提交时保存,而是在UpdateView上保存 - CreateView not saving on submit, but is on UpdateView 有没有办法组合CreateView和UpdateView? - Is there any way to combine CreateView and UpdateView? django泛型CreateView和UpdateView中的Modelformset - Modelformset in django generic CreateView and UpdateView 如何在Django中为CreateView和UpdateView创建通用模板 - How To Create Generic Templates for CreateView and UpdateView in Django 如何在Django CreateView和UpdateView中呈现Form实例? - How to render Form instance in Django CreateView & UpdateView? Django 在 CreateView 和 UpdateView 中调用自定义管理器方法 - Django call custom manager method in CreateView and UpdateView 有没有办法让updateview中的时间与django中createview中的时间保持一致 - is there a way to keep the time in updateview same as the time in createview in django 在CreateView上需要表单域的正确Django方法,而在UpdateView上是可选的? - The proper Django way to make a form field required on CreateView, but optional on UpdateView? 在 Django 中,CreateView 和 UpdateView 是否有默认模板名称? - In Django Do CreateView and UpdateView have a default template name? Django在CreateView中禁用/排除字段,但在UpdateView中启用/包含它 - Django Disable/Exclude Field in CreateView but enable/include it in UpdateView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM