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.
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.
- [Django]-How to get the app a Django model is from?
- [Django]-Django custom managers โ how do I return only objects created by the logged-in user?
- [Django]-Get name of primary field of Django model
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.
- [Django]-ReactJS with Django โ real usage
- [Django]-Django.core.exceptions.ImproperlyConfigured: Error loading psycopg module: No module named psycopg
- [Django]-Django: Rest Framework authenticate header
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.
- [Django]-Check for presence in a list django template
- [Django]-Trying to migrate in Django 1.9 โ strange SQL error "django.db.utils.OperationalError: near ")": syntax error"
- [Django]-405 "Method POST is not allowed" in Django REST framework
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.
- [Django]-Django โ Getting last object created, simultaneous filters
- [Django]-How can I fill up form with model object data?
- [Django]-PyMongo vs MongoEngine for Django
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
- [Django]-How do you perform Django database migrations when using Docker-Compose?
- [Django]-Mocking a Django Queryset in order to test a function that takes a queryset
- [Django]-Django nested transactions โ โwith transaction.atomic()โ
1๐
I have same problem on Ubuntu in a VirtualEnv to solve problem I have used this snippets.
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
- [Django]-How to use schemas in Django?
- [Django]-Django : Testing if the page has redirected to the desired url
- [Django]-Django Rest Framework โ Updating a foreign key