A
A
Alexander Proshchenkov2018-05-30 20:19:31
Django
Alexander Proshchenkov, 2018-05-30 20:19:31

How to display data from the database table on the site?

How to display data on the site in a table as shown in the picture?
The difficulty lies in the fact that the database contains duplicate data, that is, for the conditional 2018.05.30 there are three records for different people
5b0edc86a48f8576543869.png
of the model:

class School_class(models.Model):
    school_class = models.CharField(max_length=10, help_text = 'Класс')
    def __str__(self):
        return self.school_class

class People(models.Model):
    first_name = models.CharField(max_length=100, help_text = 'Имя учащегося')
    last_name = models.CharField(max_length=100, help_text = 'Фамилия учащегося')
    school_class = models.ForeignKey(School_class, help_text = 'Класс ученика', on_delete = models.SET_NULL, null=True)
    def __str__(self):
        return '{} {}'.format(self.first_name, self.last_name)

class State(models.Model):
    date = models.DateField(help_text = 'Дата')
    status = models.CharField(max_length=10, choices = (
        ('+', '+'),
        ('-', '-')
        ))
    people = models.ForeignKey(People, help_text = 'Фамилия, имя учащегося', on_delete = models.SET_NULL, null=True)

    def __str__(self):
        return '{} {}'.format(self.date, self.people.__str__())

    def get_absolute_url(self):
        return reverse('date-detail', args=[str(self.id)])

    class Meta:
        ordering = ['date']

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vikholodov, 2018-05-30
@vikholodov

people = models.ForeignKey(People, help_text = 'Фамилия, имя учащегося', on_delete = models.SET_NULL, null=True, related_name="peoples")
date_list = State.objects.all()
for obj in date_list:
    for person in obj.peoples:
        person.name
        person.status

M
ma3xak, 2018-05-30
@ma3xak

I just output with a bootstrap table, I just made a table and output the content,
Here is my example, maybe it will be useful

<table class="table">
       	<thead>
       		<tr>
       			<th>Пользователь</th>
       			<th>Дата займа</th>
       			<th>Дата погашения</th>
       			<th>Сумма займа</th>
       			<th>Количество платежей</th>
       			<th>Статус</th>
       		</tr>
       	</thead>
       	<tbody>
       		{% for user_zaim in new_zaim  %}
       		<tr scope = "row">
       			<th>{{ user_zaim.user_zaim }}</th>
       			<th>{{ user_zaim.date_zaim }}</th>
       			<th>{{ user_zaim.date_success }}</th>
       			<th>{{ user_zaim.summa_zaim }}</th>
       			<th>{{ user_zaim.platej }}</th>
       			<th>{{ user_zaim.status}}</th>      			
       		</tr>
       		{% endfor %}

       	</tbody>
       </table>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question