How can i regroup some columns in django? - django

Here is my view :
clients = models.Client.objects.all()
sellin = models.Sellinvoice.objects.filter(client_id__in = clients)
It is my template:
<table>
<tr>
<th>ClientName</th>
<th>Price</th>
</tr>
{% regroup sellin by client.name as list %}
{% for cl in list %}
<tr>
<td>{{ cl.grouper }}</td>
<td>{{ cl.client.price }}</td>
</tr>
{% endfor %}
</table>
Result is:
ClientName
Price
(if user have two invoice this column added automatically)
john doe
30000
30000 (problem)
maria
12000
david
43000
Some user have two invoice and I can regroup ClientName but how can regroup Prices?

I resolved it by adding new loop and forloop.first element to if statement:
<table>
<tr>
<th>ClientName</th>
<th>Price</th>
</tr>
{% regroup sellin by client.name as list %}
{% for cl in list %}
<tr>
<td>{{ cl.grouper }}</td>
{% for obj in cl.list %}
{% if forloop.first %}
<td>{{ obj.client.price }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
Now result is :
ClientName
Price
john doe
30000
maria
12000
david
43000

Related

Outputting "Following relationships 'backward'" to a template

I started working with Django earlier this week and watched a few tutorials and thought I would try out switching over a site I made in PHP to something a little more current. I ran into some issues over the weekend and managed to get help with some of them, but one issue remains.
I already have an existing database with some information it that I want to retrieve to display on a website. The information is modified through the database so Django doesn't have to deal with any of that - just displaying results to a bootstrapped template file.
So, in essence Django would have 2 models - one for each relevant database table.
The first model relates to products
class Bikes(models.Model):
bikempn = models.CharField(primary_key=True, max_length=50)
bikecategory = models.CharField(max_length=50)
bikeyear = models.CharField(max_length=4)
bikebrand = models.CharField(max_length=50)
bikedesc = models.CharField(max_length=255)
bikesize = models.CharField(max_length=50)
bikecolour = models.CharField(max_length=255)
bikeurl = models.CharField(max_length=255)
class Meta:
managed = False
db_table = 'bikes'
The second model relates to their expected arrival dates. This model would represent a database view which performs some logic comparing product that is on its way to the number of matching items that are on reserve.
The model is:
class Eta(models.Model):
bikempn = models.ForeignKey(Bikes, on_delete=models.CASCADE, primary_key = True, db_column='bikempn', related_name='etas')
eta = models.DateField()
class Meta:
managed = False
db_table = 'bike_eta'
The idea of this website is essentially to let people know if a product they are interested in will be arriving any time soon.
I have been trying to come up with a query that will display all of the linked information, but everything I find online hasn't quite worked. I have received some help along the way but have hit the wall again and frankly feel foolish having not figured this out yet.
So, currently I have a query that is:
kidsquery = Bikes.objects.filter(bikecategory='kids').select_related('etas')
This filters out only bikes categorized as kids bikes and should join the two models together.
My relevant template to display this information is:
{% extends 'base.html' %}
{% block content %}
{% if kidsquery %}
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered table-sm">
<thead class="table-dark">
<tr>
<th scope="col">Year</th>
<th scope="col">Brand</th>
<th scope="col">Model</th>
<th scope="col">Colour</th>
<th scope="col">Size</th>
<th scope="col">Part#</th>
<th scope="col">ETA</th>
</tr>
</thead>
{% for info in kidsquery %}
<tr>
<td>{{ info.bikeyear }}</td>
<td>{{ info.bikebrand }}</td>
{% if info.bikeurl %}
<td>{{ info.bikedesc }}</td>
{% else %}
<td>{{ info.bikedesc }}</td>
{% endif %}
<td>{{ info.bikecolour }}</td>
<td>{{ info.bikesize }}</td>
<td>{{ info.bikempn }}</td>
{% for arrival in info.etas.all %}
{% if arrival is null %}
<td>Contact Us</td>
{% else %}
<td>{{ arrival|date:"F Y" }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
{% endif %}
{% endblock %}
Does anyone know why I cannot get anything to display in the final column (where it should display the eta date values (or failing that, the "Contact Us" result)?
The proper solution to pulling the information out in the template:
{% extends 'base.html' %}
{% block content %}
{% if kidsquery %}
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered table-sm">
<thead class="table-dark">
<tr>
<th scope="col">Year</th>
<th scope="col">Brand</th>
<th scope="col">Model</th>
<th scope="col">Colour</th>
<th scope="col">Size</th>
<th scope="col">Part#</th>
<th scope="col">ETA</th>
</tr>
</thead>
{% for info in kidsquery %}
<tr>
<td>{{ info.bikeyear }}</td>
<td>{{ info.bikebrand }}</td>
{% if info.bikeurl %}
<td>{{ info.bikedesc }}</td>
{% else %}
<td>{{ info.bikedesc }}</td>
{% endif %}
<td>{{ info.bikecolour }}</td>
<td>{{ info.bikesize }}</td>
<td>{{ info.bikempn }}</td>
{% for arrival in info.etas.all %}
{% if arrival.eta %}
<td>{{ arrival.eta|date:"F Y" }} </td>
{% else %}
<td>Contact Us</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
{% endif %}
{% endblock %}
Swapped around the checks, and compensated for the lack of a date.

how to display grouped items in django templates?

I have a django template below:
{% regroup budget.productitem_set.all by group_id as grouped_product_list %}
{% for entry in grouped_product_list %}
{% for item in entry.list %}
<tbody>
<tr>
<td>{{ item.description }}</td>
<td>{{ item.quantity }}</td>
<td>{{ item.group_id }}</td>
</tr>
</tbody>
{% endfor %}
{% endfor %}
What ends up happening:
ID QUANTITY GROUP_ID
test123 23 1
test123 24 1
What output I would like
ID QUANTITY GROUP_ID
test123 23 1
24
Your also Looping description and group_Id as well using the count item
Try
{% regroup budget.productitem_set.all by group_id as grouped_product_list %}
{% for entry in grouped_product_list %}
<tbody>
<tr>
<td>{{ entry.description }}</td>
{% for item in entry.list %}
<td>{{ item.quantity }}</td>
{% endfor %}
<td>{{ entry.group_id }}</td>
</tr>
</tbody>
{% endfor %}

how to get random 1/2 data from database in jinj2 template in Django?

<table>
{% for field in fields %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endfor %}
</table>
here we will get all the data from fields . but i want to only get randomly 1/2 (i can specified how many) data in jinja2 template from backend ?
How to do this ?
<table>
{% for field in fields %}
{% if forloop.counter < x %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
just put your desired number in x,you are good to go.
Try this. You can add this logic to the frontend. This way it will display records with even ID. and those will 1/2 as well.
<table>
{% for field in fields %}
<tr>
{% if field.id%2 == 0 %}
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
{% endif %}
</tr>
{% endfor %}
</table>

Django template - How to match model datefield with a date in date generator?

In my models.py Task can have multiple Employer so I use ManyToManyField
In my views.py I got a date generator - it return range of 2 weeks from current day.
So here is my template
<table>
<thead>
<tr>
<th>{% trans 'employer_id' %}</th>
{% for i in date_range %}
<th>{{ i }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for employer in employers %}
<tr>
<td>{{ employer.employer_id }}</td>
{% for t in employer.task_set.all %}
<td>{{ t }}</td>
{% empty %}
<td>0</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
What I need is when I create a Task and assign it to employers, it should show me in the html table a start_date of Task of each employers for specific days.
So far I got this with codes above:
^date of tasks of these employers are not matching with days in the table header.
Instead of
{% for t in employer.task_set.all %}
<td>{{ t }}</td>
{% empty %}
<td>0</td>
{% endfor %}
you need to iterate over the dates again, since you're not creating <td>'s for each date.
{% for i in date_range %}
{% for t in employer.task_set.all %}
{% if t.start_date == i %}
<td>{{ t }}</td>
{% else %}
<td>-</td>
{% endif %}
{% empty %}
<td>0</td>
{% endfor %}
{% endfor %}

django template view nested list

i made a nested list and from that id like to make a table in my template
list looks something like
ground_skills_available = [[category1, [skill1, skill2, skill3]], [category1, [skill1, skill2]]]
Now i want to list it that you got category with below the items, next category and other items. Problem is, i have no clue about hot to show only category, instead of category +all items, since you cant seem to use the index of a list?
Can someone please help me out?
<table>
{% for categories in ground_skills_available %}
{% for category in categories %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
Change your outer loop to
{% for category, skills in ground_skills_available %}
This technique is described in the Django for loop docs.
On the first iteration of the loop, this will take the list [category1, [skill1, skill2, skill3], and assign
category = category1
skills = [skill1, skill2, skill3]
You can then display {{ category }}, and loop through skills.
Putting that together, you have:
<table>
{% for category, skills in ground_skills_available %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in skills %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
{% if forloop.index == 1 %} ... some code ... {% endif %}
or
{% if forloop.index0 == 0 %} ... some code ... {% endif %}
Expectation:
{% for categories in ground_skills_available %}
{% for category in categories %}
{% if forloop.first %}
<tr>
<td>{{ category }}</td>
</tr>
{% else %}
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}