简体   繁体   中英

remove empty objects from the Django query results

I want to remove those stores from the result which do not have coupons,My models.py is as follows.. This is store model and i want to remove every store which do not have any corresponding coupon data

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

coupon model is:

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)

Can any body help me please...I need to exclude all the stores which does not have related coupon data....

I prefer to set related_name explicitly:

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

Then stores, than have coupons:

stores.objects.exclude(coupons=None)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM