django combine queries on two different base models

笑着哭i 提交于 2019-12-06 14:07:43

As django doesn't support mixed querysets (they must correspond to just one model), I suggest you using a different approach: a model inheritance or FK.

class MyGeometry(models.Model):
    value = ...
    geometry = ...

class tbl_nt_123(MyGeometry):
    condition_id = models.IntegerField(blank=True, null=True)
    value = models.FloatField(blank=True, null=True)
    geometry = models.GeometryField(blank=True, null=True)

class tbl_network(MyGeometry):
    name = models.CharField(max_length=50, blank=True)
    state_id = models.IntegerField(null=True, blank=True)
    objects = models.GeoManager()

...
#you should also rewrite your queries and then merge them.
#remember that inheritance let's you access parent_links and children_links
#between models.
#your query 1 should be like this one below, and a similar approach for query2.
#since they will be of the same type, you can merge them. Later, you can access
#their children links ('tbl_nt_123' and 'tbl_network), being aware that upon non-existance those links will throw DoesNotExist, upon model iteration.

q1 = MyGeometry.objects.values_list('id', 'value', 'geometry').select_related('tbl_nt_123').filter(tbl_nt_123__restriction='height')\
.exclude(condition_id__in=tbl_conditions.objects.filter(version_id=5).values_list('condition_id',flat=True))

edit: don't know where did you get your 'restriction' field, since it does not appear in models, but i put it anyway to "respect" your code (assuming there's no error there).

edit 2: you can merge them with the methods you tried before, once the queries are of the same model (e.g. q1 + q2)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!