I've been reading the documentation and have some questions about certain behaviour.
1
Why does calling .clear() on a intermediate model, delete the intermediate models?
source>>> Membership.objects.all()
[<Membership: Membership object>, <Membership: Membership object>]
>>> beatles.members.clear()
>>> Membership.objects.all()
[]
I thinkk that the Membership models should still exist but just not be associated with the Group model.
2
Why can't you call create on a intermediate model and pass the required fields?
For example.
>>> john = Person.objects.create(name="John Lennon")
>>> beatles.members.create(person=john, date_joined=date(1960, 8, 1), invite_reason="Wanted to form a band.")
3
Why do Q objects use '&', '|' and '~' for AND, OR and NOT when python uses 'and', 'or' and 'not'?
source4
Due to how inheritance works, you have to set both pk and id to None:
django_blog.pk = None
django_blog.id = None
django_blog.save() #
django_blog.pk == 4
sourceWhy is this?
5
When using aggregate why do you get a dictionary instead of a value?
# Average price across all books.
>>> from django.db.models import Avg
>>> Book.objects.all().aggregate(Avg('price'))
{'price__avg': 34.35}
So you have to do
average_price = Book.objects.all().aggregate(Avg('price'))
['price__avg']
6.
Why do Max, Sum, etc take a string a parameter while .filter() does not? Are they inconsistent to easier tell them apart?