37

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)
    ...
GDorn
  • 8,511
  • 6
  • 38
  • 37
shanyu
  • 9,536
  • 7
  • 60
  • 68

7 Answers7

27

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 1
    thank you for the insight. it works, however there is a very painful side effect of this issue: I can't debug on pydev! it is interesting that when I take the portion out of the code, I can debug seamlessly. anty suggestions? – shanyu May 12 '09 at 12:39
  • 2
    suggestion: Don't use an IDE with dynamic languages. – muhuk May 12 '09 at 13:25
19

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.

SmileyChris
  • 10,578
  • 4
  • 40
  • 33
  • Thank you so much - those red squiggly underlines have been bugging me for ages :) – Jiaaro Dec 30 '09 at 13:39
  • Thanks for this. I was mostly just ignoring and chalked it up to Python vs. Eclipse and being a dynamic language, etc. +1 – DrBloodmoney Apr 07 '10 at 16:41
  • 1
    Glad to be of service :) Here's what my complete "consider as globals" box looks like: _,tr,DoesNotExist,MultipleObjectsReturned,base_fields,_meta,ops,outbox – SmileyChris Apr 09 '10 at 00:35
8

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.

mderk
  • 796
  • 4
  • 13
2

You can also solve it in a different way: just go to the User class, and add @DynamicAttrs in the docstring.
This will tell PyDev that attributes of the class are added dynamically, and will make it not complain any longer for "issues" like DoesNotExist.

rob
  • 36,896
  • 2
  • 55
  • 65
2

Can Eclipse resolve attributes created runtime via __metaclass__es?

Notice that you never define a DoesNotExist on any of your models and it is not defined on django.db.models.base.Model either.

muhuk
  • 15,777
  • 9
  • 59
  • 98
1

I have same problem on Ubuntu in a VirtualEnv to solve problem I have used this snippets.

http://djangosnippets.org/snippets/191/#c3091

In parituclar he make custom User Fields with code:

class UserField(forms.CharField):
    def clean(self, value):
        super(UserField, self).clean(value)
        try:
            User.objects.get(username=value)
            raise forms.ValidationError("Someone is already using this username. Please pick an other.")
        except User.DoesNotExist:
            return value
Domenico Monaco
  • 1,236
  • 12
  • 21
1

Eclipse complains that User.DoesNotExist is undefined.

What do you mean by that? Do you have python error and stack trace? This code have to work (as in documentation). Looks like an eclipse issue. Just run dev server and see if it works or not:

manage.py runserver
alex vasi
  • 5,304
  • 28
  • 31