我想从结果中删除那些没有优惠券的商店,我的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)
任何人都可以帮助我......我需要排除所有没有相关优惠券数据的商店....
答案 0 :(得分:1)
我更喜欢明确设置related_name:
class coupons(models.Model):
# ...
couponStore = models.ForeignKey(stores, on_delete=models.PROTECT,
related_name='coupons')
# ...
然后是商店,而不是优惠券:
stores.objects.exclude(coupons=None)