Fetching inherited model objects in django

后端 未结 4 802
南方客
南方客 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:21

    Querying using your "b" method, will allow for you to "bring in" all the remaining data without querying your B and C models separately. You can use the "dot lowercase model name" relation.

    http://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance

    for object in A.objects.filter(NAME__istartswith='z').order_by('ORDER'):
        if object.b:
            // do something
            pass
        elif object.c:
            // do something
            pass
    

    You may need to try and except DoesNotExist exceptions. I'm a bit rusty with my django. Good Luck.

提交回复
热议问题