如何基于常见的 M2M 属性进一步减少 SearchQuerySet

发布于 2024-12-28 16:59:23 字数 1839 浏览 3 评论 0原文

如何基于常见的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空气里的味道 2025-01-04 16:59:23

首先,为什么不


class Property(models.Model):
    name = models.CharField(unique=True, max_length=255)
    value = models.CharField(max_length=255)
    datatype = models.CharField(max_length=24, choices=DATA_CHOICES)

呢,假设上述模型:


ObjectA.objects.filter(properties__name='sky', properties__value='blue')

第三,您的对象模型不允许您在同一查询集中获取两种类型的对象。我会将 ObjectAObjectB 组合到同一个模型中。

first, why not


class Property(models.Model):
    name = models.CharField(unique=True, max_length=255)
    value = models.CharField(max_length=255)
    datatype = models.CharField(max_length=24, choices=DATA_CHOICES)

then, assuming the above model:


ObjectA.objects.filter(properties__name='sky', properties__value='blue')

third, your object model does not allow you to get both types of objects in the same query set. I would combine ObjectA and ObjectB into the same model.

眼中杀气 2025-01-04 16:59:23

像这样的东西应该可以工作

a = ObjectA.properties.filter(name = Your_name)
b = a.property_definition.filter(value = Your_Value)

也许不完全像上面的那样,但总体思路是你想将它们链接在一起并深入了解每个链中你想要的东西。

something like this should work

a = ObjectA.properties.filter(name = Your_name)
b = a.property_definition.filter(value = Your_Value)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文