B
B
bielikovv2022-03-27 16:38:39
Django
bielikovv, 2022-03-27 16:38:39

DJANGO How to implement a user's personal account so that he can add data that will be linked (or available) only to him?

I have a specific project (just starting to learn django and how site building works in general). The essence of the project is a regular site for notes or user entries. The essence of the question is that I need to understand: how to make it so that a logged in user (already implemented) can create records that will be visible only to him. I believe that this needs to be implemented through model links, but I would like to know how to do this in detail?

models(model responsible for note data)
from django.db import models
from django.urls import reverse
from django.contrib.auth.models import User

class Note(models.Model):
title = models.CharField(max_length=150, verbose_name='Title')
content = models.TextField(verbose_name='Content')
created = models.DateTimeField(auto_now_add=True, verbose_name='Created')
photo = models.ImageField(verbose_name='Image', blank=True, upload_to='photo/%Y/%m/%d')
username = models .ForeignKey(User, verbose_name='User', null=True, on_delete=models.PROTECT)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-03-27
@bielikovv

so I believe that this needs to be implemented through model links,

The connection between the user and the note is:
username = models.ForeignKey(User, verbose_name='Пользователь', null=True, on_delete=models.PROTECT)

What is the problem with selecting the username field?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question