Get data of another child in a template


Get data of another child in a template



I'm making a Board which can write comments.



All functions work well.



But the only thing I need is displaying profile of the author of the comment.




{{ board.title }}
{{ board.text }}

{% for comment is context.commentmodel_set.all %}
{{ comment.comment }}
{{ comment.whose.profile??? }} <- ????
{% endfor %}



////////////// EDIT [Add Models] ///////////


# Appname : board / model
class Board(models.Model):
whose = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField()
text = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Comment(models.Model):
post = models.ForeignKey(Board, on_delete=models.CASCADE)
whose = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
comment = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)


# Appname : accounts / model
class Profile(models.Model):
whose = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
i_need = models.CharField()
address = models.CharField()
tel = models.CharField()



/////Edit second [Views.comment] //////


@login_required
def comment(request, pk):
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.post = Board.objects.get(pk=pk)
post.whose = request.user
post.save()
return redirect('board_detail', pk)
else:
print('TurminalCheck : Invalid!!')
else:
form = CommentForm()

context = {
'form': form,
}
return render(request, 'boardapp/board.html', context)



The image for reference is attached below.
enter image description here





Can you show comment, user and profile models?
– neverwalkaloner
Jun 29 at 9:01





Wouldn't it be just {{ comment.whose.profile.i_need }}? But you should show the actual models.
– Daniel Roseman
Jun 29 at 9:09


{{ comment.whose.profile.i_need }}





@neverwalkaloner I've just updated. im using Django User system. so i don't have user model. that's all.
– Miguel
Jun 29 at 10:29





Looks like code suggested by Daniel Roseman should work. Did you try it?
– neverwalkaloner
Jun 29 at 10:32





I tried. but it doesn't work..
– Miguel
Jun 29 at 10:40




1 Answer
1



You should add related name whose field in Profile model


class Profile(models.Model):
whose = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="profile")
i_need = models.CharField()
address = models.CharField()
tel = models.CharField()



After you can call profile with {{comment.whose.profile}}





Thank you so much :) Very simple and clean!
– Miguel
23 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift