1

我想从结果中删除那些没有优惠券的商店,我的 models.py 如下.. 这是商店模型,我想删除所有没有任何相应优惠券数据的商店

class stores(models.Model):
    """ This is the store model """
    storeName = models.CharField(max_length=15)                                          # Store Name
    storeDescription = models.TextField()                                                # Store Description
    storeURL = models.URLField()                                                         # Store URL
    storePopularityNumber = models.IntegerField(choices=PRIORITY_CHOICES,default=3)      # Store Popularity Number  
    storeImage = models.ImageField(upload_to="images")                                   # Store Image 
    storeSlug = models.CharField(max_length=400)                                         # This is the text you see in the URL
    createdAt = models.DateTimeField(auto_now_add=True)                                  # Time at which store is created
    updatedAt = models.DateTimeField(auto_now=True)                                      # Time at which store is updated
    storeTags = models.ManyToManyField(tags)                                             # All the tags associated with the store

优惠券型号为:

class coupons(models.Model):
    """ This is the coupon model """
    couponTitle = models.CharField(max_length=100,default="")
    couponCode = models.CharField(max_length=30)
    couponValue = models.CharField(max_length=50)                             # Coupon value in RS.
    couponDescription = models.TextField()                                    # Coupon Description
    couponURL = models.URLField(default='http://www.foo.com')                 # Coupon click URL
    couponStore = models.ForeignKey(stores,on_delete=models.PROTECT)          # Key of coupon to store
    couponTags = models.ManyToManyField(tags)                                 # Tag names associated to coupon
    success = models.TextField(default=' ')                                   # Count of the number of times people have made it work
    failures =  models.TextField(default=' ')                                 # Count of the number of times this has failed
    lastTested = models.DateTimeField(auto_now=True)                          # When was the coupon last tested
    updatedAt = models.DateTimeField(auto_now=True)
    createdAt = models.DateTimeField(auto_now_add=True)
    addedOn = models.DateTimeField()
    active =  models.BooleanField(default=True)

任何人都可以帮助我...我需要排除所有没有相关优惠券数据的商店....

4

1 回答 1

2

我更喜欢明确设置related_name:

class coupons(models.Model):
    # ...
    couponStore = models.ForeignKey(stores, on_delete=models.PROTECT,
                                    related_name='coupons')
    # ...

然后商店,比有优惠券:

stores.objects.exclude(coupons=None)
于 2013-04-29T10:21:44.817 回答