Stack Overflow


1324 votes
What is the difference between null=True and blank=True in Django?
answers

sets (versus ) on the column in your DB. Blank values for Django field types such as or will be stored as in the DB. determines whether the field will be required in forms. This includes the admin and your custom forms. If then the field will not be required, whereas if it's the field cannot be blank. The combo of the two is so frequent because typically if you're going to allow a field to be blank in your form, you're going to also need your database to allow values for that field. The exception is s and s, which in Django are never saved as . Blank values are stored in the DB as an empty string (). A few examples: Obviously, Those two options don't make logical sense to use (though there might be a use case for if you want a field to always be required in forms, optional when dealing with an object through something like the shell.) and types are never saved as by Django, so is unnecessary. However, you can manually set one of these fields to to force set it as . If you have a scenario where that might be necessary, you should still include .

python django django-models

298 votes
How do I get the object if it exists, or None if it does not exist in Django?
19 answers

When I ask the model manager to get an object, it raises when there is no matching object. Instead of , how can I have be instead?

python django django-queryset

120 votes
What is the equivalent of "none" in django templates?
8 answers

I want to see if a field/variable is none within a Django template. What is the correct syntax for that? This is what I currently have: In the example above, what would I use to replace "null"?

python django django-forms django-templates django-views

102 votes
Django: Get an object form the DB, or 'None' if nothing matches
8 answers

Is there any Django function which will let me get an object form the database, or None if nothing matches? Right now I'm using something like: But that's not very clear, and it's messy to have everywhere.

python django

31 votes
Model.objects.get() or None
3 answers

Is there a way to accomplish the following in one call: The only way I've found a way to do this is by doing: Is there a way to do this in a single call in django? Update: There does not seem to be a way to do this other than in a block, but here is a better answer: In Django, how do I objects.get, but return None when nothing is found?

django django-models

588 votes
Find object in list that has attribute equal to some value (that meets any condition)
answers

This gets the first item from the list that matches the condition, and returns if no item matches. It's my preferred single-expression form. However, The naive loop-break version, is perfectly Pythonic -- it's concise, clear, and efficient. To make it match the behavior of the one-liner: This will assign to if you don't out of the loop.

python django list

1 votes
request.GET.get('q', None) always returns None
2 answers

I'm new to Django and I'm following the docs, I looked over and over and seems right to me but I keep getting query=None. When I input a value in the search bar goes to http://127.0.0.1:8000/search/?q=value, but the query is always None. Please help a noob getting started.

django

532 votes
How do I clone a Django model instance object and save it to the database?
answers

Just change the primary key of your object and run save(). If you want auto-generated key, set the new key to None. More on UPDATE/INSERT here. Official docs on copying model instances: https://docs.djangoproject.com/en/2.2/topics/db/queries/#copying-model-instances

python django django-models

35 votes
Django: When To Use QuerySet None
5 answers

Just came across this in the django docs Calling none() will create a queryset that never returns any objects and no query will be executed when accessing the results. A qs.none() queryset is an instance of EmptyQuerySet. I build a lot of CRUD apps (surprise) and I can't think of a situation where I would need to use . Why would one want to return an EmptyQuerySet?

python django

17 votes
Django Authenticate returns None
6 answers

I have the following code snippet: The problem is, u is always None. I've followed code samples on other stack overflow posts and have narrowed it down to the above lines. Any ideas as to what might be happening?

django authentication nonetype

1 votes
Django urlsafe_base64_decode returns None
1 answers

So I'm making an email verification system but urlsafe_base64_decode is returning None instead of the pk. Then the users clicks on a link and this is redirected to this function uidb64 returns the same as urlsafe_base64_encode(force_bytes(user.pk)).decode() but urlsafe_base64_decode(uidb64).decode() returns None. I'm using Django 2.1 by the way.

django

0 votes
python filter none values
1 answers

I have: 1. models.py 2. views.py 3. html It looks like this, right now, in my html file: Root:Manufacturing, None, None, None which is correct. However I would like not to show the ,if any, None values e.g. Root:Manufacturing

django django-views

3 votes
Django REST Framework's APIClient sends None as 'None'
3 answers

In my tests, I send mock data of models that I've passed through the serializer. The looks something like this However, the data that my API receives is formatted like which is a problem because I'm trying to specify a foreign key that is allowed to be null. Shouldn't the convert None into null instead of unicode? Is there any way to fix this or get around it? Here's my serializer And my create method in a viewset Also my model class

python django django-rest-framework

68 votes
Django - FileField check if None
2 answers

I have a model with an optional file field Let's put a value Why do I get that ? How can I check if there is a file set ?

python django django-models

90 votes
Check if OneToOneField is None in Django
8 answers

I have two models like this: I need to do something if the user has Type1 or Type2 profile: But, for users that don't have either type1 or type2 profiles, executing code like that produces the following error: How can I check the type of profile a user has? Thanks

python django django-models one-to-one

1 votes
Templatetag returns None
1 answers

My tag.py My template.html The Output of {{data3}} The output of the print in the tag.py file is a list, its exactly what I want to get saved on data3 but the code only exits None. Any sugestions?

python django templatetags

1 votes
Django Post or None
4 answers

I am trying to query with multiple filters or none I tried: and Not all parameters are required. Someone can help me?

python django

4 votes
On Heroku, locale.getdefaultlocale() is returning (None, None), breaking Django createsuperuser – how to fix?
1 answers

Trying to do a gave me an error that ended roughly: Entering a shell confirmed: locale.getdefaultlocale() is returning (None, None) on my Heroku Cedar stack. How do I set the locale such that python picks it up?

python django heroku

0 votes
Django CheckboxSelectMultiple allow none
1 answers

I need some help, I'm using a to represent a , rendered as a . All work perfect but the problem come when none checkbox was selected, it raises a validationError when ask . I need Select None, One, Many or All. It is any way to allow blank or empty? Thanks!!

django

0 votes
Django authenticate is None
3 answers

I've already set up the super user and updated the database, but the backstage password user still returns None

django django-authentication

-1 votes
None data in msg.send django
1 answers

One more question about Django Sending email by using EmailMultiAlternatives sends messages but data is 'None' My views.py looks like: After cending email, reciever recives it's message but all data is 'None' Example

python django

65 votes
Annotating a Sum results in None rather than zero
2 answers

I'm making a QA site that is similar to the page you're on right now. I'm attempting to order answers by their score, but answers which have no votes are having their score set to None rather than 0. This results in answers with no votes being at the bottom of the page below negatively ranked answers. How can I make the annotated score be zero when there are no votes for an answer? Here's my model: And here's my query: edit: And to be clear, I'd like to do this in the query. I know I could turn it into a list and then sort it in my python code, but I'd like to avoid that if possible.

django django-models django-orm

1 votes
django instance.id=None
1 answers

I'd like to save a profil photo for each user.instance.id is returning None when upload image.I'm waiting for your help. models.py froms.py views.py model_form_upload.html

python django instance

0 votes
MissingSchema: Invalid URL 'None': No schema supplied. Perhaps you meant http://None?
1 answers

views.py urls.py models.py admin.py apps.py Creating a sms-validation app.Where a user enters his phone number and message is send to validate his number. Soon after asking his number and hitting enter it shows error. raise MissingSchema(error) MissingSchema: Invalid URL 'None': No schema supplied. Perhaps you meant http://None? Traceback

python django

26 votes
Django: filtering queryset by 'field__isnull=True' or 'field=None'?
2 answers

I have to filter a queryset by a dynamic value (which can be None): may I simply write: or shall I check for None: Does the behaviour depend on the particular DBMS?

django django-models django-queryset isnull

5 votes
filter none on django field
3 answers

I have a model Person which sometimes has nothing in the field. Here is an example: How do I filter these records that have ? I have already tried the following with no success: 1: "birth_date__isnull" does not work. The following does not return the the record with id 20. How do I filter on a field being None? It seems NULL and None are different. When I see the data using sequel pro (mysql graphical client) I see "0000-00-00 00:00:00", and the following did not work either model

django django-models

3 votes
auth.authenticate() keeps returning None
1 answers

Here is my problem, I want to authenticate a custom AbstractBaseUser in. My user's informations are username: tom, password: tom. When I check in the shell, I have a SimpleUser with these informations, so it exits. Now when I print user in the django console, it prints None. But, when I look at the informations Django has, it says So from what I see, username and password are correct. What's wrong ? Edit : Creation of SimpleUser : Edit 2 : Register view in views.py : Edit 3 : Add my backends.py :

django

2 votes
Could not parse the remainder: '!=None' from '!=None' | Django Admin
1 answers

Previously static files stopped working on Django Admin (pages were unstyled), but after i logged out from admin it started giving out this error on Sign-In page: Could not parse the remainder: '!=None' from '!=None'. error on sin-in django-admin: settings.py I also didn;t make any changes to admin.

python django authentication django-templates django-admin

0 votes
Call class method or return None
4 answers

I am struggling with database relationships and Django, but have simplified my case drastically in order to get to the point. I have the following class: If I do not instantiate Car, but set nissan to None and then call get_owner, I get the following error: Is it possible to return None when calling nissan.get_owner() if nissan is None? The bigger picture: I am writing a Django templatetag that returns some data based on whether database relationships exist. It would be nice to write such code one one line though. In the end, I just want to get the value from get_owner() or get None. Is not working here...

python django

0 votes
Django None parameter function issue
0 answers

When I input none value or other values that do not match with the patientName and patientNRIC in the database(sqlite), print(patient_data) shows Response 200, then there will be error in converting the patient_data into json bcs the patient_data actually contains no data, and is not None. How should I do so that I can make sure that the function can take it None values at the same time is able to validate/make sure patient_data contains information then return true or return json otherwise return false. Below is the fun with django url. Thank you for your help.

python django sqlite