简体   繁体   中英

Django need to pivot data from 3 related models

First of all, excuse my English, I'm new in Django, and really eager to learn. I've been doing things for one year with Python. That's all my experience.

I'm trying to make a table with a list of articles listed in a column of the table, and in the other columns there is the name of some sellers at the top, then the price of the articles is listed in each sellers column with their respective price of that article.

I have this code in models.py

class Item(models.Model):      
    name = models.CharField(max_length=200)

class Seller(models.Model):    
    name = models.CharField(max_length=200)

class Price(models.Model):
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    seller = models.ForeignKey(Seller, on_delete=models.CASCADE)           
    price = models.CharField(max_length=10)

This is my views.py

def index(request):
    all_items = Item.objects.all()
    sellers = Seller.objects.all()
    prices = Price.objects.all()           
    return render(request, 'juegos/index.html',
        {'all_items': all_items,
        'sellers': sellers,
        'prices': prices}
    )

In the template i have this:

    <table>
        <thead>
            <tr>                    
                <th>Item</th>

                {% for seller in sellers %}                     
                    <th>{{ seller.name }}</th>
                {% endfor %}                                        
            </tr>
        </thead>            
        <tbody>

            {% for item in all_items %}                 
                <tr>
                <td>{{ item.name }}</td>

            <!--I don't know how to list each price for each seller-->


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

This is an example of how the information should be displayed:

这是一个例子

When I add prices in the admin, I just choose item from a previous added item, and choose seller from a list of seller i added before. So it's like everything is related, but I can't find the way to relate this objects when making this table.

I tried many things, but I started to feel like I was trying random things, so here I am.

Any hint will be appreciated.

Edit: I've been thinking that a solution could be to iterate sellers name in the table header , and then in the table body iterate each price of each item, following the order from the header interation, but i don't know what function should i use to do this. The

    <table>
        <thead>
            <tr>                    
                <th>Item</th>

                {% for seller in sellers %}                     
                    <th>{{ seller.name }}</th>
                {% endfor %}                                        
            </tr>
        </thead>            
        <tbody> 
            {% for item in all_items %}                 
                <tr>
                <td>{{ item.name }}</td>

<!-- It would be cool if i could iterate trought sellers and just put the price like this:-->           
                {% for seller in sellers %}     

                    {{ price }}  

                {% endfor %}    

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

Try this

{% for item in all_items %}                 
    <tr>
        <td>{{ item.name }}</td>
    </tr>
    {% for row in item.price_set.all %}
    <tr>
        <td>{{ row.price }}</td>
    </tr>
    {% endfor %}

{% endfor %}

You only need price

{% for p in price %}
 {{ p.item.neme }} 
 {{ p.seller.name }}
 {{ p.price }} EUR
{% endfor %}

view.py

def index(request):
    prices = Price.objects.all()           
    return render(request, 'juegos/index.html',
        {'prices': prices}
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM