Django M2M QuerySet具有多个实例和Q
我有以下模型:
class Thematics(models.Model):
title = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.title
class Pathology(models.Model):
title = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.title
class Organ(models.Model):
title = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.title
class Surgerytype(models.Model):
title = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.title
class Content(models.Model):
thematics = models.ManyToManyField(Thematics, null=True, blank=True)
pathology = models.ManyToManyField(Pathology, null=True, blank=True)
organs = models.ManyToManyField(Organ, null=True, blank=True)
surgery = models.ManyToManyField(Surgerytype, null=True, blank=True)
我有很多内容实例,它们本身有很多与它们相关的M2M实例。您可以将手术类型视为标签。
我有此查询:
content = Content.objects.all()
# surgeries is an array of Surgerytype instances titles
query = content.filter(Q(surgery__title__in=surgeries)
为简单起见,我将专注于一种M2M模型,手术类型,并承认我可以复制,然后使用其他3种模型链接查询。
我创建了3种标题:“诊断”,“手术”,“治疗性”的手术类型实例 然后,我创建了3个内容实例,并将这些M2M与手术(M2M到手术类型)的关系
- 实例1 - &GT添加到其中。添加(诊断)
- 内容实例2 - >添加(诊断,手术)
- 内容实例3 - >添加(诊断,手术,治疗)
这里的整个想法是要通过客户/医生进行动态过滤,他们可以选择标签以获取他需要的内容类型。
因此,如果他/她点击:
- 诊断且没有其他标签,医生应查看内容实例1,2和3
- 。 关于诊断,手术,治疗性或仅治疗性的BR>
- 医生应
- 在诊断和治疗方面看到内容实例3
,医生应该看到内容3
,
所以这是一个问题。
当我ajax发送给我的后端标签(即手术型实例标题)时,所有内容都返回所有包含此标签的内容的实例。现在,问题在于,如果我选择两个标签,则QuerySet将不返回,而不是所有包含这2个标签和更多标签的内容实例。我已经在内容模型中找到了1 m2m模型的方法,但它根本很肮脏且根本不可扩展(在实际内容模型中还需要添加3个其他M2M/过滤器)。
有什么想法吗?找到了许多与该主题有关的问题,但似乎没有明确的,可扩展的答案。非常感谢你们
I have the following models:
class Thematics(models.Model):
title = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.title
class Pathology(models.Model):
title = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.title
class Organ(models.Model):
title = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.title
class Surgerytype(models.Model):
title = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.title
class Content(models.Model):
thematics = models.ManyToManyField(Thematics, null=True, blank=True)
pathology = models.ManyToManyField(Pathology, null=True, blank=True)
organs = models.ManyToManyField(Organ, null=True, blank=True)
surgery = models.ManyToManyField(Surgerytype, null=True, blank=True)
I have a lot of Content instances, which themselves have a lot of M2M instances related to them. You can think of Surgerytypes as tags.
I have this query:
content = Content.objects.all()
# surgeries is an array of Surgerytype instances titles
query = content.filter(Q(surgery__title__in=surgeries)
For simplicity I will focus on one M2M model, Surgerytype, and admit that I could replicate then chain the query with the 3 other models.
I create 3 instances of Surgerytype with title: "Diagnosis", "Surgery", "Therapeutic"
I then create 3 instances of Content to which I add these M2M relations with surgery (the M2M to Surgerytype)
- Content instance 1 --> add(Diagnosis)
- Content instance 2 --> add(Diagnosis, Surgery)
- Content instance 3 --> add (Diagnosis, Surgery, Therapeutic)
The whole idea here is to have a dynamic filtering by the client/doctor who can choose tags to get to the type of content he needs.
So if he/she clicks :
- on Diagnosis and no other tag, the doctor should see Content instance 1,2 and 3.
- on Diagnosis and Surgery or just Surgery, the doctor should see Content instance 2 and 3
- on Diagnosis, Surgery, Therapeutic or just Therapeutic, the doctor should see Content instance 3
- On Diagnosis and Therapeutic, the doctor should see Content 3
So this is an AND problematic.
When I ajax send to my backend a tag (i.e. a Surgerytype instance title), all good the query returns all the instances of Content that include this tag. Now, the problem is that if I select two tags, the queryset returns nothing instead of all Content instances that include these 2 tags and possibly more tags. I have found on SO how to do this for 1 M2M model in the Content model but it is quite dirty and not scalable at all (there are 3 other M2M/filters to add in the real Content model).
Any ideas? Found many questions related to the subject but it doesn't seem to have a definitive, scalable answer. Many thanks to you all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论