Model Docs Questions

88 views
Skip to first unread message

Cody Scott

unread,
Nov 3, 2013, 2:13:34 PM11/3/13
to django-d...@googlegroups.com
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'?
source

4
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

source

Why 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?

Andre Terra

unread,
Nov 4, 2013, 10:31:37 AM11/4/13
to django-d...@googlegroups.com

On Sun, Nov 3, 2013 at 5:13 PM, Cody Scott <cody.j....@gmail.com> wrote:
3
Why do Q objects use '&', '|' and '~' for AND, OR and NOT when python uses 'and', 'or' and 'not'?
source

Ryan Hiebert

unread,
Nov 5, 2013, 12:41:41 AM11/5/13
to django-d...@googlegroups.com
On Mon, Nov 4, 2013 at 9:31 AM, Andre Terra <andre...@gmail.com> wrote:
>
> On Sun, Nov 3, 2013 at 5:13 PM, Cody Scott <cody.j....@gmail.com> wrote:
>>
>> 3
>> Why do Q objects use '&', '|' and '~' for AND, OR and NOT when python uses
>> 'and', 'or' and 'not'?
>> source
>
>
> Because Python actually does use &, | and ~.

The and/or/not operators are meant for flow control, and as such
cannot be overridden to allow us to use them for composing queries.
Reply all
Reply to author
Forward
0 new messages