I am trying to learn django rest framework and have been following the django-rest-framework tutorial. I am not able to understand why my model's owner field is taking None value instead of logged in user as it is a foreign key.
Please find my code below
my model
class Wish(models.Model):
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100,blank=True,default='')
wishtext = models.TextField()
owner = models.ForeignKey('auth.User', related_name='wishes', on_delete=models.CASCADE, null=True)
class Meta:
ordering = ('created',)
If I don't use null=True
its generating NOT NULL constraint error
my views
class WishList(generics.ListCreateAPIView):
queryset = Wish.objects.all()
serializer_class = WishSerializer
permission_classes = (IsOwnerOrReadOnly, permissions.IsAuthenticatedOrReadOnly)
# print("object reached here , permissions.IsAuthenticatedOrReadOnly")
class WishDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Wish.objects.all()
serializer_class = WishSerializer
permission_classes = (IsOwnerOrReadOnly, permissions.IsAuthenticatedOrReadOnly)
my serliazers
class WishSerializer(serializers.ModelSerializer):
owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
model = Wish
fields = '__all__'
class UserSerializer(serializers.ModelSerializer):
wishes = serializers.PrimaryKeyRelatedField(queryset=Wish.objects.all(), many=True)
class Meta:
model = User
fields = ['id', 'username', 'wishes']
my object level permission
class IsOwnerOrReadOnly(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
# Read permissions are allowed to any request,
if request.method in permissions.SAFE_METHODS:
return True
# Write permissions are only allowed to the owner of the wish.
return obj.owner == request.user
print(obj.owner)
is showing None
in the output and NULL in database, I couldn't understand the issue.
You have used serializers.ReadOnlyField
for your owner
field. This means that it will not be used when creating instances and will only be shown in serialized responses. Looking at your code you have then used this same serializer for your view inheriting from ListCreateAPIView
which is the view I assume you use for creating objects. Hence it is no mystery that your field owner
ends up being None
.
Instead if you want to use the current logged in user as the one that made the object you can use SlugRelatedField
[DRF docs] to show the username, set it to be read_only
and use pass it's default
as CurrentUserDefault
[DRF docs]:
class WishSerializer(serializers.ModelSerializer):
owner = serializers.SlugRelatedField(
# In case only the actual creator will be owner
default=serializers.CreateOnlyDefault(serializers.CurrentUserDefault()),
# In case editor can become owner
# default=serializers.CurrentUserDefault(),
read_only=True,
slug_field='username'
)
class Meta:
model = Wish
fields = '__all__'
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments