我有模型:
class VideoParameter(models.Model):
member = models.ForeignKey(Tag, on_delete=models.DO_NOTHING, related_name="parameter_tag")
value = models.PositiveIntegerField()
class Video(TimeStampedModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
parameters = models.ManyToManyField(VideoParameter, related_name="video_parameter")
url = models.URLField(verbose_name=_('Url'))
字符串
(我的意思是,视频有一些参数,如键值(例如{“兔子”:1,“Pandas”:1,“狮子”:0}))
然后,我试图获得视频,由查询参数指定。
(like此/video/?rabbit=1&panda=1&lion=0
)
我想做的SQL代码如下:
SELECT app_video.id, count(*) AS match_count FROM app_video
INNER JOIN app_video_parameters on app_video.id = app_video_parameters.video_id
INNER JOIN app_videoparameter ON app_video_parameters.videoparameter_id = app_videoparameter.id
WHERE (app_videoparameter.member_id = 1 AND app_videoparameter.value = 0)
OR (app_videoparameter.member_id = 9 AND app_videoparameter.value = 1)
OR (app_videoparameter.member_id = 11 AND app_videoparameter.value = 1)
...
GROUP BY (app_video.id)
ORDER BY match_count DESC;
型
如何在Django queryset中实现这个过程?
1条答案
按热度按时间7vhp5slm1#
你可以先构造一个
Q
对象:字符串