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!!
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
ObjectAandObjectBinto the same model.