class PostSerializer(serializers.ModelSerializer):
user = serializers.SerializerMethodField()
@staticmethod
def get_user(post):
return UserSerializer(post.user).data
def create(self, validated_data):
request = self.context['request']
user = request.user
Post= Post.objects.create(
parent=validated_data['parent_id'],
user=django_user.user,
text=validated_data['text'],
)
return Post
class Meta:
model = Post
fields = ('id', 'user', 'text')
def get_answers(self, comment):
return [CommentAnswerSerializer(comment).data for comment in comment.get_answers()]
class User(models.Model):
email = models.EmailField(default='', max_length=100)
name = models.CharField(verbose_name='Имя', max_length=100)
phone = models.CharField(
verbose_name='Телефон', blank=True, max_length=100)
class Post(models.Model):
user = models.ForeignKey(
'models.User', verbose_name='Пользователь',
null=True, related_name='posts', on_delete=models.CASCADE)
title = models.CharField(verbose_name='Заголовок',
null=True, max_length=200)
text = RichTextUploadingField(verbose_name="Текст", blank=True)