184๐
try:
user = UniversityDetails.objects.get(email=email)
except UniversityDetails.DoesNotExist:
user = None
I also see youโre storing your passwords in plaintext (a big security no-no!). Consider using the built-in auth system instead.
15๐
As mentioned in Django docs, when get
method finds no entry or finds multiple entries, it raises an exception, this is the expected behavior:
get() raises MultipleObjectsReturned if more than one object was
found. The MultipleObjectsReturned exception is an attribute of the
model class.get() raises a DoesNotExist exception if an object wasnโt found for
the given parameters. This exception is an attribute of the model
class.
Using exceptions is a way to handle this problem, but I actually donโt like the ugly try-except
block. An alternative solution, and cleaner to me, is to use the combination of filter
+ first
.
user = UniversityDetails.objects.filter(email=email).first()
When you do .first()
to an empty queryset it returns None
. This way you can have the same effect in a single line.
The only difference between catching the exception and using this method occurs when you have multiple entries, the former will raise an exception while the latter will set the first element, but as you are using get
I may assume we wonโt fall on this situation.
Note that first
method was added on Django 1.6.
- [Django]-Cannot set Django to work with smtp.gmail.com
- [Django]-Django โ SQL bulk get_or_create possible?
- [Django]-What's the best Django search app?
8๐
I also had this problem.
It was caused by the development server not deleting the django session after a debug abort in Aptana, with subsequent database deletion.
(Meaning the id of a non-existent database record was still present in the session the next time the development server started)
To resolve this during development, I used
request.session.flush()
- [Django]-How to execute a Python script from the Django shell?
- [Django]-Django-way for building a "News Feed" / "Status update" / "Activity Stream"
- [Django]-CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
4๐
You can use this in your case, it will work fine.
user = UniversityDetails.objects.filter(email=email).first()
- [Django]-How to move a model between two Django apps (Django 1.7)
- [Django]-Speeding up Django Testing
- [Django]-Django: using <select multiple> and POST
2๐
In case anybody is here and the other two solutions do not make the trick, check that what you are using to filter is what you expect:
user = UniversityDetails.objects.get(email=email)
is email a str
, or a None
? or an int
?
- [Django]-How to redirect with post data (Django)
- [Django]-How to use regex in django query
- [Django]-Get the latest record with filter in Django
2๐
You may try this way. just use a function to get your object
def get_object(self, id):
try:
return UniversityDetails.objects.get(email__exact=email)
except UniversityDetails.DoesNotExist:
return False
- [Django]-Python Asyncio in Django View
- [Django]-Django setUpTestData() vs. setUp()
- [Django]-Filtering dropdown values in django admin
-1๐
The problem is occurred when you try to match the email while the database is empty. So, if you want to solve this type of error try to put it into try except block. The example is โ
try:
user = UniversityDetails.objects.get(email=email)
except UniversityDetails.DoesNotExist:
user = None
- [Django]-How do I restrict foreign keys choices to related objects only in django
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
- [Django]-Django Model โ Get distinct value list