3

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?

9-bits
  • 10,395
  • 21
  • 61
  • 83

2 Answers2

5

Use this:

p.query.annotations.keys() 

It will give the list of all annotated fields.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Mayank
  • 51
  • 1
  • 2
0

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.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118