R
R
Ruslan Bergutov2016-03-12 10:46:28
Django
Ruslan Bergutov, 2016-03-12 10:46:28

Filtering objects in Django array?

there is a model

class Blog:
    name = некоторое поле
   
class Entry:
    blog = models.ForeignKey(Blog)
    is_published = models.BooleanField()

blogs = Blog.objects.all()

here in the blogs object you need to filter out all entries that are not published (is_published=False)
Blog.objects.filter(entries__is_published=True) фильтрует сами блоги, а не записи в них

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Lebedev, 2016-03-12
@zymanch

according to the documentation , you need to use entries__set
besides, it's not at all clear why you are trying to filter is_published=Truewhen you yourself write that you need unpublished...

O
Oscar Django, 2016-03-12
@winordie

class Blog: 
  name = некоторое поле

  def non_published_entries(self):
    return Entry.objects.filter(blog=self, is_published=False)

for blog in Blog.objects.all():
  print(blog.non_published_entries())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question