everyone, I am in the middle of a grouping of rows of a table Relationship with the same name as one. example- the table will look like
Parent_name .... Childern_name
==============
A Mittal ........ Children 1
-----------------
A Mittal ........ Children 2
-----------------
A Mittal ........ Children 3
-----------------
B Mittal .........children 1
-----------------
B Mittal .........children 2
Now I created an HTML where I want to see only
A Mittal
B Mittal
For which I have to define in views -
parentnames= Relationship.objects.distinct(Parent_name)
But it's not working kindly help. Is there any other way also to do it?
You can obtain a QuerySet
of Parent_name
s by using .values_list(…)
[Django-doc]:
parentnames= Relationship.objects.values_list(
'Parent_name', flat=True
).distinct()
That being said, by storing the names in the Relationship
, you introduce data duplication. It makes it harder to update a name. For example, if A Mittal
is renamed to C Mittal
, it will require some querying to rename all Parent_name
s and Child_name
s where A Mittal
is present. Therefore one often makes a dedicated model to store details about the object (like its name), and uses ForeignKey
s. This is also more efficient, since by default a ForeignKey
will add a database index on the corresponding column.