I want to display data from my database in given way:
User
set of emails
At this moment I have something like that:
models.py
class Person(models.Model):
name = models.CharField(max_length=50)
surname = models.CharField(max_length=50)
objects = PersonManager()
class Email(models.Model):
person= models.ForeignKey(Person,editable=False,on_delete=models.CASCADE)
email= models.EmailField()
objects = EmailManager()
def __str__(self):
return self.email
class PersonManager(models.Manager):
def get_by_natural_key(self,name,surname):
return self.get(name=name,surname=surname)
In my views.py I override get_context_data() method.
Now, in my html file I have:
{% for user in users %} //person
{% for email in user.email_set.all %}
{{ user.attr }}
{{ email }}
{% endfor %}
{% endfor %}
In this way, ordering is not by user, but by emails, for example:
insted of
user1
email1, email2
is
user1
email1
user1
email2
You must change template code. Use below code:
{% for user in users %} //person
{{ user.attr }}
{% for email in user.email_set.all %}
{{ email }}
{% endfor %}
{% endfor %}