D
D
Denis Pupchenko2017-03-30 18:24:17
Django
Denis Pupchenko, 2017-03-30 18:24:17

How to get a certain number of connected objects from the database?

models.py:

class Section(MPTTModel):
    class Meta:
        db_table='Section'
    section_text = models.CharField(verbose_name='Название раздела', max_length=100)
    parent = TreeForeignKey(verbose_name='Родительский Раздел',to='self', null=True, blank=True, related_name='children')

class Question(models.Model):
    class Meta:
        db_table = 'Question' 
    section = models.ForeignKey(Section)
    question_text = models.CharField(verbose_name='Описание вопроса', max_length=250)

views.py:
sections = Section.objects.filter(pk__in=[1,2,3])
questions = Question.objects.filter(section__in=sections)

in questions I get all the questions related to the sections.
How to get not all questions, but a certain number of questions from each section?
For example, from section c id=1, get 5 questions, from section c id=2, get 6 questions, from section c id=3, get 7 questions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-03-30
@denizzzz

Make a selection with a limit for each id separately: Nothing else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question