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

1👍

try blocks should contains a single line:

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.

classes should inherits from object or have no parenthesis:

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.

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?

class AccountCreateRecord(LoginRequiredMixin, AccountCreateOrModify,
                          CreateView):
    pass

Leave a comment