Hello, there is one database.

When displaying a table on an HTML page, it looks like this:

Applicant | Age | Height | Weight | Age | Height | Weight
Application 1 | 19 | 190 | 75 | 19 | 190 | 75
Application 1 | 19 | 190 | 75 | 19 | 190 | 75
Application 1 | 19 | 190 | 75 | 19 | 190 | 75
Application 2 | 21 | 170 | 67 | 21 | 170 | 67
Application 2 | 21 | 170 | 67 | 21 | 170 | 67
Application 2 | 21 | 170 | 67 | 21 | 170 | 67

I need to lead to this kind

Applicant | Age | Height | Weight
Application 1 | 19 | 190 | 75 |
Application 2 | 21 | 170 | 67 |

The table itself from the database looks like, application id, field, value.

The question is, can it be done by means of ORM to create a query that will convert all records with one value in (order id) into one object, or should it be done at the level of HTML code?

Now the query looks like this:

FieldsValues.objects.all() 
  • In my opinion, it is not the consequence that should be decided here, but the reason. Is it normal that there are duplicates in the database? - andreymal
  • This is not a duplicate, it is a database structure. For example, two entries from the table: 4 (ID), 17 (value), 5 (field id (for example, age). 4 (ID), 179 (value), 7 (field id (for example, height). - George

2 answers 2

The use of the conditional if statement in the template helped. Request to the database did through id.

    If you just need to remove duplicate data, you can use distinct () :

     FieldsValues.objects.distinct() 
    • Thanks, but in my case, the distinct does not work - George
    • Try to explicitly set the sorting and by what field delete matches FieldsValues.objects.order_by(field_name).distinct(field_name) - Sergey Gornostaev
    • with such a distinct, the value for the next application is taken from the first - George
    • Perhaps the matter is in complex connections through ManyToMany - George