Python / Django复杂/嵌套查询

时间:2017-08-07 05:58:39

标签: python django

我是python的初学者,Django在我的第一个项目上工作。我一步一步地克服了我遇到的几乎所有困难,但有一个问题导致我陷入困境,而且我没有更多的想法如何解决它。
目标是显示当前在特定游乐场的每个用户的孩子(孩子)。它应该是这样的:我有一个"当前访问"一个特定的用户,我想显示他/她的孩子/孩子。我不想选择哪个孩子和他/她的父母一起在操场上玩耍,在这个阶段我假设整个可能的孩子都和他/她的父母一起去了:)

以下是我的模特:

    class Quarter(models.Model):
    name = models.CharField(max_length=64, choices=QUARTER, default='not defined')

class Pground(models.Model):
    place = models.CharField(max_length=128)
    description = models.TextField()
    quarter = models.ForeignKey(Quarter)

class Child(models.Model):
    name = models.CharField(max_length=128)
    age = models.IntegerField(choices=AGE, default=-1)
    sex = models.IntegerField(choices=SEX, default=1)
    whose_child = models.ForeignKey(User)


class Parent(models.Model):
    user = models.OneToOneField(User)
    quarter = models.ForeignKey(Quarter)
    children = models.ManyToManyField(Child)   


class Visit(models.Model):
    who = models.ForeignKey(User)
    pground = models.ForeignKey(Pground)
    time_from = models.DateTimeField()
    time_to = models.DateTimeField()

相关的观点到现在为止:

        class HomeLogView(LoginRequiredMixin, View):
    login_url = '/login/'
    def get(self, request):
        user = request.user
        quarter = user.parent.quarter
        pgrounds = quarter.pground_set.all()
        current_visits = {}
        for pground in pgrounds:
            now = datetime.now()
            current_visits[pground] = pground.visit_set.filter(time_from__lte=now, time_to__gte=now)
            current_visits[pground] = {}
            this_visits = pground.visit_set.filter(time_from__lte=now, time_to__gte=now)
            for visit in this_visits:
                current_visits[pground][visit] = Child.objects.filter(whose_child=visit.who)
                this_children = Child.objects.filter(whose_child=visit.who)

        ctx = {'user': user, 'quarter': quarter, 'pgrounds': pgrounds, 'current_visits': current_visits, 'this_visits': this_visits, 'this_children': this_children}
        return TemplateResponse(request, 'home_login.html', ctx)

所以使用模板(部分):

<dt>{% for pground, current_visits in current_visits.items %}
            <dd><h3>{{pground.place}}</h3></dd>
                {% for visit in current_visits %}
            <dd>Użytkownik: <mark>{{visit.who}}</mark> na placyku od: {{visit.time_from}}
                do: {{visit.time_to}} {{visit.pground}} <a href="/new_message/{{visit.who.id}}">Send message</a></dd>
                {% for visit, this_visits in current_visits.items %}
                    {% for child in this_visits %}
                        {{child.name}} {{child.age}}
                    {% endfor %}
                {% endfor %}                   
                {% endfor %}
            {% endfor %}
        </dt>

我让整个孩子出现在每次访问的特定游乐场。但我的目标是以这种模式显示信息:对于每个&#34;当前访问&#34;,基于父母,他/她的孩子/孩子。

我真的很感激如何从我被卡住的地方搬走的建议。预先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

在父/子模型中设置related_name,如

class Child(models.Model):
     name = models.CharField(max_length=128)
     age = models.IntegerField(choices=AGE, default=-1)
     sex = models.IntegerField(choices=SEX, default=1)
     whose_child = models.ForeignKey(User, related_name='children')

在您查询时,您可以修改您的查询,如:

this_visits = pground.visit_set.filter(time_from__lte=now, time_to__gte=now).prefetch_related('parent__children')

假设父母和孩子是你的相关名字。