I want that any user could create their own products with my RESTful API. For this purpose, I created a view that inherits ListCreateAPIView. The problem is that a user should only create products that he/she own, so when the instance of the Product model is created, I wanted that the field Owner corresponds to the user that is authenticated.
Here's my Product model
class Product(models.Model):
owner = models.ForeignKey(User)
name = models.CharField(max_length=30)
Obviously my serializer is:
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = models.Product
and my view:
class ProductView(generics.ListCreateAPIView):
queryset = models.Product.objects.all()
serializer_class = serializers.ProductSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
#serializer.initial_data['owners'] = models.Person.objects.get(user__email=request.user).user_id
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
As you can see, I tried modifying the "initial_data", but it is a QueryDict, which is inmutable. Another option is transforming it into a Python dict and then adding the "owner" value, but at doing that I get every value as a list (i.e. {'name': ['MyName']}), so it would be a very ugly way to solve the problem, I think it should be a more straightforward solution.