9-bits
9-bits

Reputation: 10765

Where are django's annotated fields stored on the model instance?

I am doing this:

p = MyModel.objects.filter(user__username="me").annotate(friend_count=Count(friends))

when I look at:

p[0]._meta.get_all_field_names()

It returns everything defined on the model but not the annotated field 'friend_count'

Is there a function I can use to see all the annotated fields of a particular model instance?

Upvotes: 3

Views: 2255

Answers (2)

Mayank
Mayank

Reputation: 61

Use this:

p.query.annotations.keys() 

It will give the list of all annotated fields.

Upvotes: 6

bruno desthuilliers
bruno desthuilliers

Reputation: 77892

annotations are just stored as plain instance attributes (just like the values for the ORM fields FWIW). You can use dir(my_model_instance) to see all the attributes (class and instance ones) names, or my_model_instance.__dict__.keys() for the plain instance attributes only.

Upvotes: 0

Related Questions