Django Rest Framework bulk updates inserting instead of updating
Date : March 29 2020, 07:55 AM
|
Django REST Framework partial updates with depth
Date : March 29 2020, 07:55 AM
I wish did fix the issue. This was originally answered in the question, but I feel it deserves a decent explanation. In Django REST Framework 3, the process of saving nested data has been made considerably more clear. Now you can override update on the serializer and manipulate the validated_data, updating your model objects manually. This is specifically documented now, but the implementation is typically very specific to the situation. class Users(models.Model):
user = models.OneToOneField(User)
foo = models.CharField()
baz = models.CharField()
yin = models.CharField()
yang = models.CharField()
def save(self, *args, **kwargs):
user_ = self.user
user_.save(update_fields=('username', 'first_name', 'last_name'), **kwargs)
return super(Users, self).save(*args, **kwargs)
|
Trying to bulk upload to django-rest-framework (ModelViewSets) using django.client
Date : March 29 2020, 07:55 AM
wish of those help Yeah, Bulk uploading is possible in django rest framework. You can use Django REST Framework Bulk. BulkListSerializer,
BulkSerializerMixin,
ListBulkCreateUpdateDestroyAPIView,
class Meta(object):
model = FooModel
# only necessary in DRF3
list_serializer_class = BulkListSerializer
|
In the Django REST framework, how to allow partial updates when using a ModeViewSet?
Tag : python , By : nonkelhans
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You don't need to adapt the serializer in any way. With that viewset, any call to the "detail" endpoint using the PATCH method will do a partial update. The Django Rest Framework ModelViewSet base class includes the following mixin. Here you can see how partial=True is passed when calling partial_update, which is routed to the PATCH method by default: class UpdateModelMixin(object):
"""
Update a model instance.
"""
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
# If 'prefetch_related' has been applied to a queryset, we need to
# refresh the instance from the database.
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
def perform_update(self, serializer):
serializer.save()
def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)
|
Django rest framework bulk: Correct URL syntax for bulk operations?
Tag : django , By : Steven Weber
Date : October 07 2020, 03:00 PM
wish help you to fix your issue I'm attempting to carry out bulk DELETEs/PUTs on my Django API, mymodel inherits from MultipleDBModelViewSet which in turn inherits from the BulkModelViewSet , There are two things they are saying at their documentation. url(r'foo/', ...)
url(r'foo/(?P<pk>\d+)/', ...)
class FooView(BulkDestroyAPIView):
def allow_bulk_destroy(self, qs, filtered):
# custom logic here
# default checks if the qs was filtered
# qs comes from self.get_queryset()
# filtered comes from self.filter_queryset(qs)
return qs is not filtered
class SimpleViewSet(generics.BulkModelViewSet):
def filter_queryset(self, queryset):
ids = self.request.query_params.get('ids')
if ids:
return queryset.filter(id__in=ids.split(',')
# returns normal query set if no param
return queryset
|