Django Model属性-计算模型的多个实例的外键 [英] Django Model Property - Count foreign key for multiple instances of model

查看:57
本文介绍了Django Model属性-计算模型的多个实例的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我的教科书模型中具有属性NumWishes和NumPosts。但是,这些属性仅返回当前教科书的计数。我希望该属性返回当前教科书isbn的所有教科书的计数。当前这不起作用,因为模型中有 unique_together =('isbn','class_name')

Currently I have the properties NumWishes and NumPosts inside my Textbook model. However, these properties are only returning the count for the current textbook. I would like the property to return the count for all textbooks with the current textbook isbn. This doesn't work currently because I have unique_together = ('isbn', 'class_name')in the model.

例如,如果我有3本教科书具有相同的isbn但3种不同的类,则该属性将为每本返回1。但是,我希望该属性为每个教科书返回3。

For example, if I have 3 textbooks with the same isbn but 3 different classes the property will return 1 for each of them. However, I want the property to return 3 for each textbook.

class Textbook(models.Model):
    textbook_name = models.CharField(max_length=200)
    class_name = models.CharField(max_length=200)
    author = models.CharField(max_length=200)
    isbn = models.CharField(max_length=200)
    semester = models.CharField(max_length=200, default="FALL2015")

# Properties for determing supply and demand
    @property
    def NumWishes(self):
        return self.wishlist_set.count()

    @property
    def NumPosts(self):
        return self.posting_set.count()

    @property
    def DemSup(self):
        if (self.posting_set.count() != 0):
            showmethemoney = float((self.wishlist_set.count()))/(self.posting_set.count())
        else:
            showmethemoney = 0
            return showmethemoney

# Instead of a pk field isbn and class_name together have to be unique
    class Meta:
        unique_together = ('isbn', 'class_name')

    def __str__(self):
        return self.textbook_name

我的发布模型如下。愿望清单模型与发布模型非常相似。

My posting model is below. The wishlist model is very similar to the posting one.

class Posting(models.Model):
    textbook = models.ForeignKey(Textbook)
    condition = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    user = models.ForeignKey(User)
    image = models.ImageField(upload_to='postingpics/%Y/%m/%d', default="/textchange/nophoto.png")
    post_date = models.DateTimeField('date_posted')
    comments = models.CharField(max_length=50, default="")

    def __str__(self):
        return str(self.textbook)

    def was_posted_recently(self):
        return self.post_date >= timezone.now() - datetime.timedelta(days=1)
    was_posted_recently.admin_order_field = 'post_date'
    was_posted_recently.boolean = True
    was_posted_recently.short_description = 'Posted recently'


Thanks :)

推荐答案

如果我在评论中写的内容是正确的,则可以这样做:

If what I wrote in the comment is true, you can just do:

@property
def NumPosts(self):
    return Posting.objects.filter(textbook__isbn=self.isbn).count()

,但是如果您在循环中使用该属性,效率将非常低下。

but this is going to be very inefficient if you are using the property within a loop.

这篇关于Django Model属性-计算模型的多个实例的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆