Fetching inherited model objects in django

后端 未结 4 828
南方客
南方客 2021-01-02 15:50

I have a django application with the following model:

Object A is a simple object extending from Model with a few fields, and let\'s say, a particul

4条回答
  •  既然无缘
    2021-01-02 16:20

    If A can be concrete, you can do this all in one query using select_related.

    from django.db import connection
    q = A.objects.filter(NAME__istartswith='z').order_by('ORDER').select_related('b', 'c')
    for obj in q:
       obj = obj.b or obj.c or obj
       print repr(obj), obj.__dict__ # (to prove the subclass-specific attributes exist)
    print "query count:", len(connection.queries)
    

提交回复
热议问题