Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django -- User.DoesNotExist does not exist?

I'm trying to get hold of Django. I use Pydev on Eclipse. I have written a simple signup page that I can't get to work. Eclipse complains that User.DoesNotExist is undefined. Most likely, I am missing something trivial. Here's the relevant portion of the code:

from django.contrib.auth.models import User
...
class SignUpForm (forms.Form):
    ...
    def clean_username (self): 
        try:
            User.objects.get(username=self.cleaned_data['username'])
        except User.DoesNotExist:
            return self.cleaned_data['username']
        raise forms.ValidationError(USERNAME_ALREADY_IN_USE)
    ...
like image 571
shanyu Avatar asked May 12 '09 07:05

shanyu


3 Answers

The problem is really with PyDev, not your code. What you have done is absolutely correct, but IDEs will always have difficulty resolving attributes in a dynamic language like Python. In the case of the DoesNotExist exception, it is added via a __metaclass__ rather than through normal object inheritance, so PyDev is unlikely to be able to find it. However, it should definitely work.

like image 131
Daniel Roseman Avatar answered Sep 20 '22 09:09

Daniel Roseman


I just discovered Pydev actually has a nice workaround for this.

Go to Window > Preferences, then Pydev > Editor > Code Analysis.

Click the Undefined tab and add "DoesNotExist" to the text box titled Consider the following names as globals.

like image 40
SmileyChris Avatar answered Sep 22 '22 09:09

SmileyChris


Pydev has a workaround for such cases (when the members are defined at runtime). Just add #@UndefinedVariable at the end of the string which cause the warning (or ctrl+1 on keyboard when the cursor is at "DoesNotExist"), and it won't complain.

like image 24
mderk Avatar answered Sep 19 '22 09:09

mderk