I am converting my function-based views to class-based views. Following the official documentation, I was getting errors using the Response imported from rest_framework. When using HttpResponse it's working fine. The error I am getting with Response is:
.accepted_renderer not set on Response
This is my view:
def get(self, request):
users = User.objects.all()
serializer = UserSerializer(users, many=True)
return Response(serializer.data)
What could be the reason for this?
serializer:
class Meta:
model = User
fields = '__all__'
instead of this:
return Response(serializer.data)
try this:
return Response(serializer.data, status=status.HTTP_200_OK) #Status is depends on your code
I think your serializer is not saved so that's why you are getting that error
def get(self, request):
users = User.objects.all()
serializer = UserSerializer(users, data=request.data, many=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Final thing try this.
Is there a way to set request_body for #swagger_auto_schema to only be part of a Serializer? The reasoning being as you can see below the creator is set by the current user object passed by authenticator to the post_create view, but if I set request_body to the PostSerializer that'll be confusing for others, because they'll assume it needs a creator attribute, even though that's parsed from the user. Is there a way I can set request_body for this endpoint that uses #api_view with some of PostSerializer?
view.py
#api_view(['POST'])
#swagger_auto_schema(
operation_description="Create a post object"
)
def post_create(request):
try:
request.data['creator'] = str(request.user.uuid)
post_serializer = PostSerializer(data=request.data)
if post_serializer.is_valid(raise_exception=True):
post_obj = post_serializer.save()
except ValidationError as e:
return Response(dict(error=str(e),
user_message=error_message_generic),
status=status.HTTP_400_BAD_REQUEST)
return Response(post_serializer.data, status=status.HTTP_201_CREATED)
serializer.py
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('creator', 'body', 'uuid', 'created', 'updated_at')
One way of doing it is to implement a second Serializer with only the concerned subset of fields, Which would look like this :
class PostSerializerCreator(serializers.ModelSerializer):
class Meta:
model = Post
# Only include the user-provided fields
fields = ('body', 'uuid', 'created', 'updated_at')
#api_view(['POST'])
#swagger_auto_schema(
request_body=PostSerializerCreator,
operation_description="Create a post object"
)
def post_create(request):
try:
request.data['creator'] = str(request.user.uuid)
post_serializer = PostSerializer(data=request.data)
if post_serializer.is_valid(raise_exception=True):
post_obj = post_serializer.save()
except ValidationError as e:
return Response(dict(error=str(e),
user_message=error_message_generic),
status=status.HTTP_400_BAD_REQUEST)
return Response(post_serializer.data, status=status.HTTP_201_CREATED)
I suppose you should also remove other fields such as updated_at which should be computed at execution rather than being supplied by the API user.
I've tried something like this, it does not work.
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
def save(self):
user = self.context['request.user']
title = self.validated_data['title']
article = self.validated_data['article']
I need a way of being able to access request.user from my Serializer class.
You cannot access the request.user directly. You need to access the request object, and then fetch the user attribute.
Like this:
user = self.context['request'].user
Or to be more safe,
user = None
request = self.context.get("request")
if request and hasattr(request, "user"):
user = request.user
More on extra context can be read here
Actually, you don't have to bother with context. There is a much better way to do it:
from rest_framework.fields import CurrentUserDefault
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
def save(self):
user = CurrentUserDefault() # <= magic!
title = self.validated_data['title']
article = self.validated_data['article']
As Igor mentioned in other answer, you can use CurrentUserDefault. If you do not want to override save method just for this, then use doc:
from rest_framework import serializers
class PostSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CurrentUserDefault())
class Meta:
model = Post
CurrentUserDefault
A default class that can be used to represent the current user. In order to use this, the 'request' must have been provided as part of the context dictionary when instantiating the serializer.
in views.py
serializer = UploadFilesSerializer(data=request.data, context={'request': request})
This is example to pass request
in serializers.py
owner = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)
Source From Rest Framework
Use this code in view:
serializer = UploadFilesSerializer(data=request.data, context={'request': request})
then access it with this in serializer:
user = self.context.get("request").user
For those who used Django's ORM and added the user as a foreign key, they will need to include the user's entire object, and I was only able to do this in the create method and removing the mandatory field:
class PostSerializer(serializers.ModelSerializer):
def create(self, validated_data):
request = self.context.get("request")
post = Post()
post.title = validated_data['title']
post.article = validated_data['article']
post.user = request.user
post.save()
return post
class Meta:
model = Post
fields = '__all__'
extra_kwargs = {'user': {'required': False}}
You can pass request.user when calling .save(...) inside a view:
class EventSerializer(serializers.ModelSerializer):
class Meta:
model = models.Event
exclude = ['user']
class EventView(APIView):
def post(self, request):
es = EventSerializer(data=request.data)
if es.is_valid():
es.save(user=self.request.user)
return Response(status=status.HTTP_201_CREATED)
return Response(data=es.errors, status=status.HTTP_400_BAD_REQUEST)
This is the model:
class Event(models.Model):
user = models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
date = models.DateTimeField(default=timezone.now)
place = models.CharField(max_length=255)
You can not access self.context.user directly. First you have to pass the context inside you serializer. For this follow steps bellow:
Some where inside your api view:
class ApiView(views.APIView):
def get(self, request):
items = Item.object.all()
return Response(
ItemSerializer(
items,
many=True,
context=request # <- this line (pass the request as context)
).data
)
Then inside your serializer:
class ItemSerializer(serializers.ModelSerializer):
current_user = serializers.SerializerMethodField('get_user')
class Meta:
model = Item
fields = (
'id',
'name',
'current_user',
)
def get_user(self, obj):
request = self.context
return request.user # <- here is current your user
In GET method:
Add context={'user': request.user} in the View class:
class ContentView(generics.ListAPIView):
def get(self, request, format=None):
content_list = <Respective-Model>.objects.all()
serializer = ContentSerializer(content_list, many=True,
context={'user': request.user})
Get it in the Serializer class method:
class ContentSerializer(serializers.ModelSerializer):
rate = serializers.SerializerMethodField()
def get_rate(self, instance):
user = self.context.get("user")
...
...
In POST method:
Follow other answers (e.g. Max's answer).
You need a small edit in your serializer:
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
def save(self):
user = self.context['request'].user
title = self.validated_data['title']
article = self.validated_data['article']
Here is an example, using Model mixing viewsets. In create method you can find the proper way of calling the serializer. get_serializer method fills the context dictionary properly. If you need to use a different serializer then defined on the viewset, see the update method on how to initiate the serializer with context dictionary, which also passes the request object to serializer.
class SignupViewSet(mixins.UpdateModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet):
http_method_names = ["put", "post"]
serializer_class = PostSerializer
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
kwargs['context'] = self.get_serializer_context()
serializer = PostSerializer(instance, data=request.data, partial=partial, **kwargs)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
return Response(serializer.data)
The solution can be simple for this however I tried accessing using self.contenxt['request'].user but not working in the serializer.
If you're using DRF obviously login via token is the only source or maybe others that's debatable.
Moving toward a solution.
Pass the request.user instance while creating serializer.create
views.py
if serializer.is_valid():
watch = serializer.create(serializer.data, request.user)
serializer.py
def create(self, validated_data, usr):
return Watch.objects.create(user=usr, movie=movie_obj, action=validated_data['action'])
If you are using generic views and you want to inject current user at the point of saving the instance then you can override perform_create or perform_update:
def perform_create(self, serializer):
serializer.save(user=self.request.user)
user will be added as an attribute to kwargs and you can access it through validated_data in serializer
user = validated_data['user']
drf srz page
in my project it worked my user field was read only so i needed to get
user id in the create method
class CommentSerializer(serializers.ModelSerializer):
comment_replis = RecursiveField(many=True, read_only=True)
user = UserSerializer(read_only=True)
class Meta:
model = PostComment
fields = ('_all_')
def create(self, validated_data):
post = PostComment.objects.create(**validated_data)
print(self._dict_['_kwargs']['data']["user"]) # geting #request.data["user"] # <- mian code
post.user=User.objects.get(id=self._dict_['_kwargs']['data']["user"])
return post
in my project i tried this way and it work
The best way to get current user inside serializer is like this.
AnySerializer(data={
'example_id': id
}, context={'request': request})
This has to be written in views.py
And now in Serializer.py part
user = serializers.CharField(default=serializers.CurrentUserDefault())
This "user" must be your field in Model as any relation like foreign key
I'm using django 1.11.5 and python 3.5.
Using rest-framework, I want to search a patient having uid.
When I'm trying to have a serializer with only one field I get the error (1048, "Column 'yearofbirth' cannot be null").
I don't want to save anything, I just want to get all user's info having the given uid.
Is there any solution to fix this?
I believe that occurs because I'm using post method. How to use get via browser and not using curl?
serializers.py
class GetUserSerializer(serializers.ModelSerializer):
id = serializers.CharField(source='uid')
class Meta:
model = User
fields = ('id', )
views.py
class GetUser(CreateAPIView):
permission_classes = ()
serializer_class = GetUserSerializer
def get(self, request):
serializer = GetUserSerializer(data=request.data)
# Check format and unique constraint
if not serializer.is_valid():
return Response(serializer.errors, \
status=status.HTTP_400_BAD_REQUEST)
data = serializer.data
if User.objects.filter(uid = data['id']).exists():
user = User.objects.get(uid = data['id'])
resp = {"user":{"uid":user.uid, "firstname":user.firstname, "yearofbirth": user.yearofbirth, \
"lastnames": user.lastname, "othernames": user.othernames}}
return Response(resp, status=status.HTTP_200_OK)
else:
resp = {"error": "User not found"}
return Response(resp, status=status.HTTP_404_NOT_FOUND)
models.py
class User(models.Model):
uid = models.CharField(max_length=255, unique=True,default="0")
firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255, blank=True)
othernames = models.CharField(max_length=255, blank=True)
yearofbirth = models.SmallIntegerField(validators=[MinValueValidator(1900),
MaxValueValidator(2018)], null = False
Making a GET request through the browsable API actually makes a few additional calls behind the scenes, with different methods. If your serializer isn't equipped to handle them it'll cause an error like you're seeing here. Check out this SO post for more detail: Django Rest Framework: `get_serializer_class` called several times, with wrong value of request method
I created a simple model API with Django Rest Framework.. For the most part i followed quick start tutorial on DRF site.
I am accessing it from angular app using Authorization token. Everything works fine except when i wish to make a new post...
This is the error I get null value in column "owner_id" violates not-null constraint
This is my code:
models.py
class Post(models.Model)
title = models.CharField(max_length=100, blank=False)
point = models.PointField(max_length=255, null=True)
owner = models.ForeignKey('auth.User', related_name='posts')
active = models.NullBooleanField()
created = models.DateTimeField(auto_now_add=True)
expires = models.DateTimeField(blank=True, null=True)
serializers.py
class PostSerializer(serializers.ModelSerializer):
owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
model = Post
fields = ( 'owner', 'id', 'title', 'point', 'active', 'created', 'expires')
views.py
class PostList(APIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def post(self, request, format=None):
serializer = PostSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
Thanks.
OK, lets take this one step at a time. First, perform_create() method is defined in the create mixin that is used by ModelViewSet. Overriding it in the APIView derived view class doesn't do anything. So your Post creation is all handled by the post() method. The easiest way to fix the error is to add owner=self.request.user to your serializer.save() call in post().
That said, you might want to consider redefining the whole thing as a ModelViewSet, since you will likely need a full set of CRUD APIs anyway.
class PostViewSet(viewsets.ModelViewSet):
class Meta:
queryset = Post.objects.all()
serializer_class = PostSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def perform_create(self, serializer):
serializer.save(user=self.request.user)