pass user data to serializer in nested serializers when creating object in django rest framework

When User tries to add an Announcement, should i pass all the informations of the user in the form ? i’m using token authentification. So for adding an Announcement the user must be authenticated.

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



When trying the browsable API. to create a new announcement i have to enter all the informations of the user. But if the user is already authenticated. is there any solution to create the announcement for only this user and show it to the other users ?

Answer

If you don’t want to create a User when creating an Announcement, omit the author field from your AnnouncementSerializer, then pass the current user when saving serializer object:

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)