Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Django查询结果中删除空对象_Python_Django_Django Orm - Fatal编程技术网

Python 从Django查询结果中删除空对象

Python 从Django查询结果中删除空对象,python,django,django-orm,Python,Django,Django Orm,我想从结果中删除那些没有优惠券的商店,我的models.py如下。。 这是商店模型,我想删除所有没有任何相应优惠券数据的商店 class stores(models.Model): """ This is the store model """ storeName = models.CharField(max_length=15) # Store Name storeDescription

我想从结果中删除那些没有优惠券的商店,我的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)

请任何机构帮助我…我需要排除所有没有相关优惠券数据的商店…

我更喜欢明确设置相关的商店名称:

class coupons(models.Model):
    # ...
    couponStore = models.ForeignKey(stores, on_delete=models.PROTECT,
                                    related_name='coupons')
    # ...
然后商店,比有优惠券:

stores.objects.exclude(coupons=None)

不,我不能这样做,因为我有其他相关的模型要存储,我没有在上面显示这些模型,以使问题切中要害……我正在通过此查询查询…存储相关优惠券数据:
def storecoups(self):for t in stores.objects.filter(storeName=self.storeName):return“,”。join([str(a.couponTitle)对于in t.coups_set.all())
是他们的任何类似概念,可以从查询结果中删除没有相关优惠券的商店……Myabe您需要这样的东西:
def storecoups(self):for t in stores.objects.exclude(coups=None)。filter(storeName=self.storeName):return“,”。join([str(a.couponTitle)对于t.tuops.all()])
中的一个,假设
couponStore
字段具有
相关的\u name='tuops'
可以显示整个模型定义吗?或者只是将相关的名称重命名为smth-else,例如
related\u-name='store\u-coupons'
好吧,我没有看到任何错误,这可能会发生。尝试将相关的\u名称设置为smth(例如
优惠券
),并在任何地方使用它而不是
优惠券集
,或者不设置相关的\u名称并将
优惠券集
用作字段引用,但在查询中仍然使用
优惠券
:stores.objects.exclude(优惠券=无)。