Django -在model .py中过滤QuerySet

时间:2021-11-21 21:54:27

Is is possible to make queries from django models?

是否可以对django模型进行查询?

I have 2 models:

我有两个模型:

class Book(models.Model):
    ...
    def copies_available(self):
        pass

and

class BookCopy(models.Model):
    ...
    book_category = models.ForeignKey(Book, related_name='copies')
    issued_to = models.ForeignKey(EndUser, related_name='issued_books', null=True, blank=True)

I want copies_available() to return the number of BookCopy intances of that Book whose issued_to is None

我想要copies_available()返回那本书的书签意图的数量,它的发行数是None

3 个解决方案

#1


2  

This should work:

这应该工作:

class Book(models.Model):
    ...
    def copied_available(self):
        return self.copies.filter(issued_to__isnull=True).count()

#2


0  

Yes just set limit_choices_to in the ForeignKey. From the docs:

是的,在ForeignKey中设置limit_choices_to。从文档:

"A dictionary of lookup arguments and values (see Making queries) that limit the available admin or ModelForm choices for this object. Use this with functions from the Python datetime module to limit choices of objects by date. For example:"

“查找参数和值的字典(参见“查询”),它限制了该对象的可用管理或模型形式选择。使用Python datetime模块中的函数来限制对象的日期选择。例如:“

limit_choices_to = {'pub_date__lte': datetime.date.today}

#3


0  

I did this a couple years ago, so it may have changed a little bit:

我几年前做过这个,所以它可能有一点变化:

You nee to create a BookManager class, and put the functionality there. Then assign the manager to your Book model's object variable, like this:

您需要创建一个BookManager类,并将功能放在其中。然后将管理器分配给图书模型的对象变量,如下所示:

class BookManager(models.Manager):

    def copied_available(self):

        queryset = BookCopy.objects.filter(book_category=self.id).filter(issued_to is not None)

        return queryset.count()

class Book(models.Model):
    ...
    objects = BookManager()

So in your template, you can do something like:

所以在你的模板中,你可以这样做:

<p> Copies: {{ thebook.copied_available }}</p>

#1


2  

This should work:

这应该工作:

class Book(models.Model):
    ...
    def copied_available(self):
        return self.copies.filter(issued_to__isnull=True).count()

#2


0  

Yes just set limit_choices_to in the ForeignKey. From the docs:

是的,在ForeignKey中设置limit_choices_to。从文档:

"A dictionary of lookup arguments and values (see Making queries) that limit the available admin or ModelForm choices for this object. Use this with functions from the Python datetime module to limit choices of objects by date. For example:"

“查找参数和值的字典(参见“查询”),它限制了该对象的可用管理或模型形式选择。使用Python datetime模块中的函数来限制对象的日期选择。例如:“

limit_choices_to = {'pub_date__lte': datetime.date.today}

#3


0  

I did this a couple years ago, so it may have changed a little bit:

我几年前做过这个,所以它可能有一点变化:

You nee to create a BookManager class, and put the functionality there. Then assign the manager to your Book model's object variable, like this:

您需要创建一个BookManager类,并将功能放在其中。然后将管理器分配给图书模型的对象变量,如下所示:

class BookManager(models.Manager):

    def copied_available(self):

        queryset = BookCopy.objects.filter(book_category=self.id).filter(issued_to is not None)

        return queryset.count()

class Book(models.Model):
    ...
    objects = BookManager()

So in your template, you can do something like:

所以在你的模板中,你可以这样做:

<p> Copies: {{ thebook.copied_available }}</p>