django按两级外键关系过滤

时间:2015-09-22 21:17:14

标签: django filter relationship

class AAA(models.Model):
    name = CharField

class BBB(models.Model):
    aaa = models.ForeignKey(AAA)
    name = CharField

class CCC(model.Model):
    bbb = models.ForeignKey(BBB)

如何在视图中创建过滤器,我按照AAA名称过滤CCC对象?

我知道如何为CCC创建过滤器以按BBB名称进行过滤:

CCC.objects.filter(bbb__name=request.bbb.name)

但我现在知道如果有意义的话,如何为两级关系创建一个适合的人?

是吗:

CCC.objects.filter(aaa__name = request.bbb.aaa.name)?

编辑:

models.py

class Stage(models.Model):
    project = models.ForeignKey(Project)
    slug = models.SlugField(unique=True)
    stage_number = models.CharField(max_length = 10)
    stage_name = models.CharField(max_length = 10)
    zones = models.ManyToManyField(Zone, through='ZoneStage')


class SubStage(models.Model):
    sub_name = models.CharField(max_length=10)
    stage = models.ForeignKey(Stage)
    zones = models.ManyToManyField(Zone, through='ZoneSubStage')


class ZoneSubStage(models.Model):
    zone = models.ForeignKey(Zone)
    substage = models.ForeignKey(SubStage)
    value = models.PositiveSmallIntegerField(default=0)
    slug = models.SlugField(unique=True)

views.py

这就是我使用过滤器的地方:

class ZoneSubStageUpdate(UpdateView):
    form_class = ZoneSubStageForm
    model = ZoneSubStage
    template_name = 'autostages/zonesubstage_update.html'


    def get_context_data(self, **kwargs):
        context = super(ZoneSubStageUpdate, self).get_context_data(**kwargs)
        context['slug'] = ZoneSubStage.objects.filter(substage__stage__slug=request.substage.stage.slug)
        return context    

编辑2:

在我的views.py中,我有以下观点:

class ProjectStageDetailView(generic.DetailView):
    template_name = 'autostages/projectstagedetail.html'
    queryset = Stage.objects.all()
    context_object_name = 'stage'


    def get_context_data(self, **kwargs):
        context = super(ProjectStageDetailView, self).get_context_data(**kwargs)
        context['projects'] = Project.objects.all()
        # tutaj dac filtr aby filtrowac substage od 'stage' 
       # context['subs'] = SubStage.objects.all()
        context['subs'] = SubStage.objects.filter(stage__slug=self.kwargs['slug'])
        return context   

TEMPLATE

' projectstagedetail.html'

<table border="1">
    <tr>
        <td style="width:40px" >


        <!-- print zones -->            
        {% for project in projects %}
        {% if project == stage.project %}
        {% for zone in project.zone_set.all|dictsort:"zone_name" %}
        <td style="width:40px" align="center">{{ zone.zone_name }}</td>
        {% endfor %}
        {% endif %}
        {% endfor %}  
    </tr>       


    {% for substage in subs %}
    <tr>  
        {% if substage.stage == stage %}
        <td style="width:40px">{{ substage.sub_name }}</td>
        {% else %}
        <td style="width:40px">NONE</td>
        {% endif %}



    {% for project in projects %}
    {% if project == stage.project %}
        {% for zone in project.zone_set.all|dictsort:"zone_name" %}

        <td style="width:40px" align="center">  

{% for zss in substage.zonesubstage_set.all %}
                  {% if zss.zone_id == zone.id %} 

                  <a href="{% url 'projects:zonesubstage_edit' zss.pk %}">{{ zss.value }} {{ zss.substage.stage.slug }}</a>

                    {% endif %}

                    {% endfor %}
                    </td>
{% endfor %}
{% endif %}
{% endfor %}

        </tr>
    {% endfor %}
    </table>

1 个答案:

答案 0 :(得分:3)

请记住,您始终可以使用下划线符号来关注关系(&#39; __&#39;)

尝试:

# assuming that request.bbb.aaa.name has the value aaa.name must match
CCC.objects.filter(bbb__aaa__name=request.bbb.aaa.name)