2012-01-24 1 views
0

一般的なM2Mアトリビュートに基づいてSearchQuerySetをさらに減らしてsky = blueのすべてのオブジェクトをクエリできますか?一般的なM2Mアトリビュートに基づいてSearchQuerySetをさらに減らすには

割り当てをObjectA:

Property → property_definition = "sky" value = "blue" 
Property → property_definition = "current_color" value = "red" 

割り当てをObjectB:

Property → property_definition = "sky" value = "red" 
Property → property_definition = "current_color" value = "blue" 

これは、唯一の答え(ObjectAに)を生じるはずであるが、テンプレートには、両方の性質を見ているので、私は2を取得しています。

これらの結果をどのように絞り込むかについては、誰でも光を当てることができます。 SearchQuerySetはremove()をサポートしていないので、それらを処理することはできません。これを行うためのきれいな方法は考えられませんか?

ヘルプPlease 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 %} 

ありがとうございます!このような

答えて

1

まず、なぜ


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を同じモデルに組み合わせます。

0

何かがそうでないかもしれない、正確に上記のような

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

を動作するはずですが、一般的な考え方は、あなたがtogethorそれらをチェーンして、各鎖に欲しいものを得るためにドリルダウンしたいです。

関連する問題