2017-07-31 67 views
0

我有機型Django的過濾器或沒有記錄

class Product(models.Model): 
    title = models.CharField(...) 

class PropertyType(models.Model): 
    title = models.CharField(...) 

class Property(models.Model): 
    property_type = models.ForeignKey(PropertyType) 
    product = models.ForeignKey(Product, related_name='properties') 
    value = models.CharField(...) 

所以,我怎麼就可以篩選產品與一些價值和房產類型的屬性,包括產品,具有相同的房產類型

東西沒有財產這樣

properties = Property.objects.filter(property_type__pk=12, value='anyValue') 
products = Product.objects.filter(Q(properties__in=properties)| 
       Q(NO PROPERTY RECORD WITH PROPERTYTYPE__ID=12 FOR PRODUCT)) 

回答

0

通過經驗找到了答案其實

properties = Property.objects.filter(property_type__pk=12, value='anyValue') 
products = Product.objects.filter(Q(properties__in=properties)| 
      ~Q(properties__property_type__pk__in=[12])) 
+0

笑一個我說什麼,反正如果我的回答幫你,請voteup –

0

正確的做法是properties = Property.objects.filter(property_type__pk__contains=12, value='anyValue')我不明白的第二