class AAA(models.Model):
name = CharField
class BBB(models.Model):
aaa = models.ForeignKey(AAA)
name = CharField
class CCC(model.Model):
bbb = models.ForeignKey(BBB)
how to create a filter in views in which I filter CCC objects by AAA name ?
I know how to create filter for CCC to filter by BBB name:
CCC.objects.filter(bbb__name=request.bbb.name)
but I do now know how to create a fiter for two level relationship if it makes sense ?
is it:
CCC.objects.filter(aaa__name=request.bbb.aaa.name) ?
EDIT:
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
and this is where I use my filter:
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
EDIT 2:
In my views.py I have following view:
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>
Remember you can always follow relationships using underscore notation ('__
')
Try:
# assuming that request.bbb.aaa.name has the value aaa.name must match
CCC.objects.filter(bbb__aaa__name=request.bbb.aaa.name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With