Questions about Relational Models in Django

Asked

Viewed 29 times

1

Hello,

I’m creating a Blog system with Django, in it I have several Models: Profile(which receives an auth.User), Post(which has an Author field that receives a Profile). I am developing a page to perform the update of a Post, I know that the user.is_authenticated function checks if the user is connected, in my case how do I know if the user who is connected is the same as the Post

Follow the code in my template:

{% user.is_authenticated and user.id == post.user %}
   ......
{%endif%}

In case it would return me an Object Profile and not User, how do I know the User profile, it would be correct to use post.user.profile profile ?

1 answer

0

It will depend on how the relationships between your models are structured, to compare if the user is the same who made the post you need to compare the request user with the linked post.

By your description it seems to me that the path would post > Author > user, because by the description "the post has an Author field that has a link with auth. User" then would be like this:

{% user.is_authenticated and user == post.author.user %}
   ...
{%endif%}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.