在 django rest 框架中创建对象时,将用户数据传递给嵌套序列化程序中的序列化程序
pass user data to serializer in nested serializers when creating object in django rest framework
当用户尝试添加公告时,我是否应该在表单中传递用户的所有信息?
我正在使用令牌身份验证。
因此,要添加公告,用户必须经过身份验证。
Models.py
class User(AbstractUser):
username = None
email = models.EmailField(max_length=100, verbose_name='email',
unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
class Announcement(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
photo = models.ManyToManyField(Photo, blank=True)
class Photo(models.Model):
name = models.CharField(max_length=100)
content_type = models.CharField(max_length=100)
path = models.CharField(max_length=100)
class Parameter(models.Model):
name = models.CharField(max_length=100)
value = models.FloatField(blank=True, null=True)
announcement = models.ForeignKey(
Announcement,related_name='parameters', on_delete=models.CASCADE)
Serializers.py
class AnnouncementSerializer(serializers.ModelSerializer):
author = UserSerializer(required=True)
parameters = ParameterSerializer(many=True,
required=False)
photo = PhotoSerializer(many=True,
required=False)
class Meta:
model = Announcement
fields = ['id', 'name', 'author',
'parameters', 'photo']
class UserSerializer(serializers.ModelSerializer):
photo = PhotoSerializer()
class Meta:
model = User
fields = ['id', 'email','photo', ]
class ParameterSerializer(serializers.ModelSerializer):
class Meta:
model = Parameter
fields = '__all__'
class PhotoSerializer(serializers.ModelSerializer):
class Meta:
model = Photo
fields = '__all__'
Views.py
class AnnouncementCreate(CreateAPIView):
permission_classes = [IsAuthenticated]
queryset = models.Announcement.objects.all()
serializer_class = AnnouncementSerializer
尝试浏览时API。要创建新公告,我必须输入用户的所有信息。但是如果用户已经通过身份验证。是否有任何解决方案可以仅为该用户创建公告并将其显示给其他用户?
如果您不想在创建 Announcement
时创建 User
,请省略 AnnouncementSerializer
中的 author
字段,然后在保存序列化器对象:
serializer.py
class AnnouncementSerializer(serializers.ModelSerializer):
parameters = ParameterSerializer(many=True, required=False)
photo = PhotoSerializer(many=True, required=False)
class Meta:
model = Announcement
fields = ['id', 'name', 'parameters', 'photo']
views.py
class AnnouncementCreate(CreateAPIView):
permission_classes = [IsAuthenticated]
queryset = models.Announcement.objects.all()
serializer_class = AnnouncementSerializer
def perform_create(self, serializer):
serializer.save(author=self.request.user)
当用户尝试添加公告时,我是否应该在表单中传递用户的所有信息? 我正在使用令牌身份验证。 因此,要添加公告,用户必须经过身份验证。
Models.py
class User(AbstractUser):
username = None
email = models.EmailField(max_length=100, verbose_name='email',
unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
class Announcement(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
photo = models.ManyToManyField(Photo, blank=True)
class Photo(models.Model):
name = models.CharField(max_length=100)
content_type = models.CharField(max_length=100)
path = models.CharField(max_length=100)
class Parameter(models.Model):
name = models.CharField(max_length=100)
value = models.FloatField(blank=True, null=True)
announcement = models.ForeignKey(
Announcement,related_name='parameters', on_delete=models.CASCADE)
Serializers.py
class AnnouncementSerializer(serializers.ModelSerializer):
author = UserSerializer(required=True)
parameters = ParameterSerializer(many=True,
required=False)
photo = PhotoSerializer(many=True,
required=False)
class Meta:
model = Announcement
fields = ['id', 'name', 'author',
'parameters', 'photo']
class UserSerializer(serializers.ModelSerializer):
photo = PhotoSerializer()
class Meta:
model = User
fields = ['id', 'email','photo', ]
class ParameterSerializer(serializers.ModelSerializer):
class Meta:
model = Parameter
fields = '__all__'
class PhotoSerializer(serializers.ModelSerializer):
class Meta:
model = Photo
fields = '__all__'
Views.py
class AnnouncementCreate(CreateAPIView):
permission_classes = [IsAuthenticated]
queryset = models.Announcement.objects.all()
serializer_class = AnnouncementSerializer
尝试浏览时API。要创建新公告,我必须输入用户的所有信息。但是如果用户已经通过身份验证。是否有任何解决方案可以仅为该用户创建公告并将其显示给其他用户?
如果您不想在创建 Announcement
时创建 User
,请省略 AnnouncementSerializer
中的 author
字段,然后在保存序列化器对象:
serializer.py
class AnnouncementSerializer(serializers.ModelSerializer):
parameters = ParameterSerializer(many=True, required=False)
photo = PhotoSerializer(many=True, required=False)
class Meta:
model = Announcement
fields = ['id', 'name', 'parameters', 'photo']
views.py
class AnnouncementCreate(CreateAPIView):
permission_classes = [IsAuthenticated]
queryset = models.Announcement.objects.all()
serializer_class = AnnouncementSerializer
def perform_create(self, serializer):
serializer.save(author=self.request.user)