question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NestedViewSetMixin suport for saving

See original GitHub issue

Is looks like using the NestedViewSetMixin auto generates the filter code for nested routers, but does nothing for saving.

I would recommend something like the following for a URL like companies/company_pk/locations/location_pk:

    def pre_save(self, obj):
        obj.location = Location.objects.get(pk=self.kwargs['location_slug'],
                                    company__pk=self.kwargs['company_pk']
                                    )

to be auto generated so that a user posting to the above URL can omit those fields which are determined by the URL itself.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:24 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
neokreecommented, Mar 5, 2015

@chibisov I don’t know if there is a way to find the model from a related field, but I suppose yes.

def create(self, request, *args, **kwargs):
        parents_query = self.get_parents_query_dict()
        request_data = {}

        # insert normal datas
        for key,value in request.data.items():
            request_data[key] = value

        # insert parents datas
        for key,value in parents_query.items():
            request_data[key] = Model.objects.get(pk=value) # instead of 'Model' should be the Foreign key related model object

        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)

You should add create and update override in NestedViewSetMixin.

I have copied rest_framework implementation of create then I added related datas from parents_query_dict to a new dict and used it in get_serializer instead the base request.data

Edit another way to do this is move all to the serializer create and update

1reaction
neokreecommented, Mar 5, 2015

Hi! Why not simple call the self.get_parents_query_dict() for getting all url values?

Edit I’m overriding the base write requests for getting that work. Maybe my work can be included into the extensions to insert automatically url datas in request.data

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Rest Framework extensions documentation
class TaskViewSet(NestedViewSetMixin, ModelViewSet): model = TaskModel class ... Django's save method has keyword argument update_fields.
Read more >
Django REST Framework extensions - DRF - pointborn
Extensions for serializers functionality. PartialUpdateSerializerMixin. By default every saving of ModelSerializer saves the whole object. Even ...
Read more >
alanjds/drf-nested-routers - Gitter
Hi @jogilder. Glad that you worked this out. I was starting to bisect thru the release versions to see which one stopped to...
Read more >
Django-filter Choice Filter does not recognize an existing field ...
class StartUpViewSet(NestedViewSetMixin, ModelViewSet): ... Field does not exist · Django - Which file types does Django's image field support / not support ......
Read more >
The Ultimate Tutorial For Django Rest Framework - Sunscrapers
views.py class BorrowedViewset(NestedViewSetMixin, viewsets. ... only for reading and adding, the saving option will require more work.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found