In my views I am filtering all parent parent objects. support_object = Contact.objects.filter(user=user,parent__isnull=True)
. In my html I am showing all parent objects but how to show if any parent have child objects. I want to show all child objects under it’s parent.
html
{%for i in support_object%}
parent{{i.name}}
parent{{i.message}}
#here I want to show all child message of parent
{%endfor%}
Can You show your models in order to understand how is the relations (or reverse relation) between parent and child.
Rigo-Villalta I will update very soon. please allow me few minutes
Assuming that you have a model class “Message” with a user as author you should loop over this again, but it can give you the n+1 problems:
{%for i in support_object%}
parent{{i.name}}
parent{{i.message}}
{%for m in i.contact_parent%}
{{ m.message }}
{%endfor%}
{%endfor%}
but you have to substitute “message” in “{{ m.message }}” by the name of the field in the class “Messages”
getting this error 'RelatedManager' object is not iterable
after using your code.
models.py :
class Contact(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True,related_name='contact_user')
parent =models.ForeignKey('self', on_delete=models.CASCADE,
null=True, blank=True, related_name='contact_parent')
sno = models.AutoField(primary_key=True,)
I am having a very similar issue. Did you resolve the “object is not iterable” issue?>
If you’ve got a specific problem for which you would like assistance, I’d suggest you open a new issue with all the relevent details (most likely the models, view, and template involved, along with the complete traceback of any errors you may be receiving).