Answer the question
In order to leave comments, you need to log in
Is there a more elegant solution?
Imagine that there are two models models.py
I need to list all the stores, but under each list the phone numbers belonging to it. Solved 2 days. And here's how I solved it...
class Store(models.Model):
name = models.CharField(max_length=200)
address = models.CharField(max_length=200)
class Phones(models.Model):
phone = models.CharField(max_length=50)
store = models.ForeignKey(Store)
from django.shortcuts import render
from .models import Store, Phones
def get_store():
stores = []
for line in Store.objects.all():
phone = line.phones_set.filter(show=1)
l = []
l.append(line)
l.append(phone)
stores.append(l)
return stores
def index(request):
template = 'myhomechita/page.html'
context = { 'stores': get_store,}
return render(request, template, context)
{% if stores %}
<div class="footer-col-2">
<div class="col-name">Наши магазины</div>
<ul class='stores'>
{% for block in stores %}
<li>
{% for unit in block %}
{% if forloop.counter == 1 %}
<span>{{ unit.name }} - {{ unit.address }}</span>
{% else %}
<ul>
{% for phone in unit %}
<li>{{ phone.name }}: {{ phone.phone }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</li>
{% endfor %}
</ul>
</div>
{% endif %}
Answer the question
In order to leave comments, you need to log in
It is not very clear why you are creating a list, you can simply pass a queryset to the template, and do the rest of the work in the template, because you can cycle through the queryset in the template. The template will end up looking like this:
{% if stores_queryset %}
<div class="footer-col-2">
<div class="col-name">Наши магазины</div>
<ul class='stores'>
{% for store in stores_queryset %}
<li>
<span>{{ store.name }} - {{ store.address }}</span>
<ul>
{% for phone in store. phones_set %}
<li>{{ phone.name }}: {{ phone.phone }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
class Store(models.Model):
name = models.CharField(max_length=200)
address = models.CharField(max_length=200)
@property
def showed_phones(self):
return self.phones_set.filter(show=1)
'stores': Store.objects.all()
{% if stores %}
<div class="footer-col-2">
<div class="col-name">Наши магазины</div>
<ul class='stores'>
{% for store in stores_queryset %}
<li>
<span>{{ store.name }} - {{ store.address }}</span>
<ul>
{% for phone in store.phones_set %}
<li>{{ phone.name }}: {{ phone.phone }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
In the store attribute of the Phones model, add related_name:
class Phones(models.Model):
phone = models.CharField(max_length=50)
store = models.ForeignKey(Store, related_name='phone_number')
store = Store.objects.get(pk=1)
phone_num = store.phone_number.phone
{% if stores %}
<div class="footer-col-2">
<div class="col-name">Наши магазины</div>
<ul class='stores'>
{% for store in stores %}
<li>
<span>{{ store.name }} - {{ store.address }}</span>
<ul>
<li>{{ store.phone_number.name }}: {{ store.phone_number.phone }}</li>
</ul>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question