Questions tagged [django-models]
For questions concerning use of the model class from the web framework Django.
38,983
questions
1184votes
24answers
388kviews
What is the difference between null=True and blank=True in Django?
When we add a database field in django we generally write:
models.CharField(max_length=100, null=True, blank=True)
The same is done with ForeignKey, DecimalField etc. What is the basic difference in ...
843votes
17answers
536kviews
How do I do a not equal in Django queryset filtering?
In Django model QuerySets, I see that there is a __gt and __lt for comparative values, but is there a __ne or != (not equals)? I want to filter out using a not equals. For example, for
Model:
bool ...
751votes
10answers
756kviews
How can I temporarily disable a foreign key constraint in MySQL?
Is it possible to temporarily disable constraints in MySQL?
I have two Django models, each with a foreign key to the other one. Deleting instances of a model returns an error because of the foreign ...
712votes
13answers
227kviews
What is a "slug" in Django?
When I read Django code I often see in models what is called a "slug". I am not quite sure what this is, but I do know it has something to do with URLs. How and when is this slug-thing supposed to be ...
611votes
11answers
372kviews
What does on_delete do on Django models?
I'm quite familiar with Django, but I recently noticed there exists an on_delete=models.CASCADE option with the models. I have searched for the documentation for the same, but I couldn't find anything ...
544votes
9answers
419kviews
How to filter empty or NULL names in a QuerySet?
I have first_name, last_name & alias (optional) which I need to search for. So, I need a query to give me all the names that have an alias set.
Only if I could do:
Name.objects.filter(alias!="")
...
521votes
11answers
161kviews
What's the difference between django OneToOneField and ForeignKey?
What's the difference between Django OneToOneField and ForeignKey?
511votes
15answers
281kviews
Extending the User model with custom fields in Django
What's the best way to extend the User model (bundled with Django's authentication app) with custom fields? I would also possibly like to use the email as the username (for authentication purposes).
...
502votes
4answers
169kviews
How to define two fields "unique" as couple
Is there a way to define a couple of fields as unique in Django?
I have a table of volumes (of journals) and I don't want more then one volume number for the same journal.
class Volume(models.Model):...
431votes
12answers
417kviews
How to query as GROUP BY in django?
I query a model:
Members.objects.all()
And it returns:
Eric, Salesman, X-Shop
Freddie, Manager, X2-Shop
Teddy, Salesman, X2-Shop
Sean, Manager, X2-Shop
What I want is to know the best Django way ...
417votes
3answers
215kviews
What's the difference between select_related and prefetch_related in Django ORM?
In Django doc,
select_related() "follows" foreign-key relationships, selecting additional related-object data when it executes its query.
prefetch_related() does a separate lookup for each ...
405votes
9answers
434kviews
How to delete a record in Django models?
I want to delete a particular record. Such as
delete from table_name where id = 1;
How can I do this in a django model?
385votes
16answers
276kviews
Convert Django Model object to dict with all of the fields intact
How does one convert a django Model object to a dict with all of its fields? All ideally includes foreign keys and fields with editable=False.
Let me elaborate. Let's say I have a django model like ...
374votes
15answers
202kviews
Can "list_display" in a Django ModelAdmin display attributes of ForeignKey fields?
I have a Person model that has a foreign key relationship to Book, which has a number of fields, but I'm most concerned about author (a standard CharField).
With that being said, in my PersonAdmin ...
367votes
5answers
281kviews
Django Model() vs Model.objects.create()
What it the difference between running two commands:
foo = FooModel()
and
bar = BarModel.objects.create()
Does the second one immediately create a BarModel in the database, while for FooModel, the ...