12π
I had a similar problem and I solved it by explicitly creating and passing a new instance to the serializer. In the UserVoteViewSet
you have to substitute perform_create
with create
:
def create(self, request, *args, **kwargs):
uv = UserVote(created_by=self.request.user)
serializer = self.serializer_class(uv, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
7π
I was able to solve this with one-liner in views.py
def create(self, request, *args, **kwargs):
request.data.update({'created_by': request.user.id})
return super(UserVoteViewSet, self).create(request, *args, **kwargs)
Since this view expects user to be authenticated, donβt forget to extend permission_classes
for rest_framework.permissions.IsAuthenticated
- [Django]-Timestamp fields in django
- [Django]-Django :How to integrate Django Rest framework in an existing application?
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
2π
The other weird way you can do is use signals like this
@receiver(pre_save, sender=UserVote)
def intercept_UserVote(sender, instance, *args, **kwargs):
import inspect
for frame_record in inspect.stack():
if frame_record[3]=='get_response':
request = frame_record[0].f_locals['request']
break
else:
request = None
instance.pre_save(request)
Then basically you can define pre_save in your model
def pre_save(self, request):
# do some other stuff
# Although it shouldn't happen but handle the case if request is None
self.created_by = request.user
The advantage of this system is you can use same bit of code for every model. If you need to change anything just change in pre_save()
. You can add more stuff as well
- [Django]-Django model one foreign key to many tables
- [Django]-Django :How to integrate Django Rest framework in an existing application?
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
0π
Add the following to the ViewSet
:
def perform_create(self, serializer):
serializer.save(user=self.request.user)
And the following on the Serializer
:
class Meta:
extra_kwargs = {
'user': {
'required': False,
},
}
- [Django]-Django: show the count of related objects in admin list_display
- [Django]-Itertools.groupby in a django template
- [Django]-Check if celery beat is up and running
-1π
Below code worked for me.
Even I was facing same error after many experiments found something, so added all fields in serializer.py
in class meta, as shown below β
class Emp_UniSerializer( serializers.ModelSerializer ):
class Meta:
model = table
fields = '__all__' # To fetch For All Fields
extra_kwargs = {'std_code': {'required': False},'uni_code': {'required': False},'last_name': {'required': False},'first_name': {'required': False}}
Here, we can update any field which are in "extra_kwargs"
, it wont show error ["This field is required."]
- [Django]-Django character set with MySQL weirdness
- [Django]-Filtering dropdown values in django admin
- [Django]-Django: Use of DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT in settings.py?