class Post(models.Model): post_author = models.ForeignKey(User, blank=True, null=True, verbose_name="Автор", ) post_name = models.CharField(max_length=200,blank=False, db_index=True, unique=True, default="",verbose_name='Название поста') class Comment(models.Model): comment_post = models.ForeignKey(Post,blank=True, null=True, verbose_name='Пост',related_name='com' ) comment_text = models.TextField(blank=False, verbose_name='Текст комментария') comment_nik = models.ForeignKey(User, blank=True, null=True, verbose_name="автор",) 

How to attach comment_nik to request.user

  class PostInline(admin.StackedInline): model = Comment fields = ['comment_nik','comment_text'] readonly_fields = ['comment_nik', ] # нельзя изменить поле extra = 2 class PostAdmin(admin.ModelAdmin): --..-- inlines = [PostInline,] 

    1 answer 1

    What does binding mean? Do you want in comment_nik to store the name of the user who created the comment? Your comment_nik is a ForeignKey on a User model. Is it your model or from django.contrib.auth.models?

    • User is a model from django.contrib.auth.models, yes. I tied to the Post model like this: 'def save_model (self, request, obj, form, change): instance = form.save (commit = False) # when saving, add the author's model to the post_author field if it is empty user = request. user if not instance.post_author: instance.post_author = user instance.save () if not instance.com: instance.com = user return instance 'how to also bind to Inline - Andrey Neobyasnimyy
    • Code code, have respect. - Mikhail Sidorov