D
D
Dmitry2015-06-03 16:25:05
Django
Dmitry, 2015-06-03 16:25:05

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)

views.py
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)

Template Part
{% 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

3 answer(s)
Y
Yuri Shikanov, 2015-06-03
@pyHammer

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 %}

About filtering phones, taking into account show=1. You can make a custom manager , and then only filtered phones will be in phones_set.
Or easier - make a property in the Store model:
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)

and then in the template, in the appropriate place, you need to use the cycle {% for phone in store. showed_phones %}

D
Dmitry, 2015-06-03
@pyHammer

'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 %}

I did this, but it doesn't output anything {% for store in stores_queryset %}

I
Ivan, 2015-06-05
@ATNC

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')

After that, you can reach the phone from the Store model like this:
store = Store.objects.get(pk=1)
phone_num = store.phone_number.phone

You can also use the related name in templates. Something like this...
{% 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 question

Ask a Question

731 491 924 answers to any question