如何基于常见的 M2M 属性进一步减少 SearchQuerySet
如何基于常见的 M2M 属性进一步减少 SearchQuerySet,以便可以查询天空 = 蓝色的所有对象?
分配对象A:
Property → property_definition = "sky" value = "blue"
Property → property_definition = "current_color" value = "red"
分配对象B:
Property → property_definition = "sky" value = "red"
Property → property_definition = "current_color" value = "blue"
这应该会产生一个且只有一个答案(对象A),但我得到了2,因为模板看到了这两个属性。
任何人都可以阐明如何缩小这些结果的范围。因为 SearchQuerySet 不支持 remove() 我无法对它们进行后期处理,并且我想不出一种干净的方法来做到这一点?
请帮助!
--- models.py ---
DATA_CHOICES = ((u'string', u'string'), (u'integer', u'integer'), (u'real', u'real'), (u'boolean', u'boolean'))
class PropertyDefinition(models.Model):
name = models.CharField(unique=True, max_length=255)
datatype = models.CharField(max_length=24, choices=DATA_CHOICES)
class Property(models.Model):
property_definition = models.ForeignKey(PropertyDefinition)
value = models.CharField(max_length=255)
class ObjectA(models.Model):
properties = model.ManyToManyField(Property)
name = models.CharField(unique=True, max_length=255)
class ObjectB(models.Model):
properties = model.ManyToManyField(Property)
name = models.CharField(unique=True, max_length=255)
--- search_indexes.py ---
class ObjectAIndex(indexes.BasicSearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
name = indexes.CharField(model_attr='name')
def get_model(self):
return ObjectA
class ObjectBIndex(ObjectAIndex):
def get_model(self):
return ObjectB
--templates (identical but named appropriately )--
{{object.name}}
{% for property in object.properties %}
{{ property.property_definition.name }} {{ property.value }}
{% endfor %}
非常感谢!!
How do I further reduce a SearchQuerySet based on a common M2M attribute such that I can query for all objects where sky = blue?
Assign ObjectA:
Property → property_definition = "sky" value = "blue"
Property → property_definition = "current_color" value = "red"
Assign ObjectB:
Property → property_definition = "sky" value = "red"
Property → property_definition = "current_color" value = "blue"
This should result in one and only one answer (ObjectA) but I'm getting 2 because the templates are seeing both properties.
Can anyone shed some light as to how to narrow these results. Because SearchQuerySet doesn't suppport remove() I can't post process them and I can't think of a clean way to do this??
Help Please!!
--- models.py ---
DATA_CHOICES = ((u'string', u'string'), (u'integer', u'integer'), (u'real', u'real'), (u'boolean', u'boolean'))
class PropertyDefinition(models.Model):
name = models.CharField(unique=True, max_length=255)
datatype = models.CharField(max_length=24, choices=DATA_CHOICES)
class Property(models.Model):
property_definition = models.ForeignKey(PropertyDefinition)
value = models.CharField(max_length=255)
class ObjectA(models.Model):
properties = model.ManyToManyField(Property)
name = models.CharField(unique=True, max_length=255)
class ObjectB(models.Model):
properties = model.ManyToManyField(Property)
name = models.CharField(unique=True, max_length=255)
--- search_indexes.py ---
class ObjectAIndex(indexes.BasicSearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
name = indexes.CharField(model_attr='name')
def get_model(self):
return ObjectA
class ObjectBIndex(ObjectAIndex):
def get_model(self):
return ObjectB
--templates (identical but named appropriately )--
{{object.name}}
{% for property in object.properties %}
{{ property.property_definition.name }} {{ property.value }}
{% endfor %}
Thanks much!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,为什么不
呢,假设上述模型:
第三,您的对象模型不允许您在同一查询集中获取两种类型的对象。我会将
ObjectA
和ObjectB
组合到同一个模型中。first, why not
then, assuming the above model:
third, your object model does not allow you to get both types of objects in the same query set. I would combine
ObjectA
andObjectB
into the same model.像这样的东西应该可以工作
也许不完全像上面的那样,但总体思路是你想将它们链接在一起并深入了解每个链中你想要的东西。
something like this should work
Maybe not exactly like the above, but the general idea is you want to chain them togethor and drill down to get what you want in each chain.