0

I have an api for updating the object. But update form shows empty field. How can I show form filled with current value? If I want to show current value filled in just django, I would do formname(instance=object). How can I do similar in DRF when using RetrieveAPIView.

class RestaurantUpdateAPI(RetrieveAPIView):
    queryset = Restaurant.objects.all()
    serializer_class = RestaurantCreateUpdateSerializer

    def perform_update(self, serializer):
        print('serializer',serializer)
        instance = serializer.save()
        # send_email_confirmation(user=self.request.user, modified=instance)

This code gives me empty form. I want the name field should have name of restaurant, city should have city name and so on.

How can this be done in DRF RetrieveAPIView?

enter image description here

Cœur
  • 32,421
  • 21
  • 173
  • 232
Serenity
  • 3,000
  • 4
  • 20
  • 59

1 Answers1

0

You api url is ../edit/..., so I think you wanna use this api to edit your restaurant.(btw, REST API should not use "edit" in it, you just use http method to handle this). In your case, if you want use get method and post method at the same time.You should use RetrieveUpdateDestroyAPIView

Used for read-write-delete endpoints to represent a single model instance.

Provides get, put, patch and delete method handlers.

Extends: GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin

Community
  • 1
  • 1
Windsooon
  • 5,568
  • 3
  • 26
  • 45
  • I will look at it. There will be only one view for udpate and destroy and i should define all get put and delete method. That means i will have only one url for update and delete. – Serenity Sep 04 '16 at 14:41
  • Yes, it's easy to maintain with only one url. – Windsooon Sep 04 '16 at 14:48
  • I am confuse with what url should be given. Because one url does both update and delete work. – Serenity Sep 04 '16 at 15:05
  • You can use url like api/restaurant to handle CRUD( post method to create, get method to list, put method to update and delete method to delete).check this out https://stackoverflow.com/questions/671118/what-exactly-is-restful-programming – Windsooon Sep 04 '16 at 15:08
  • But the serializer class will be different in my case for get and post, update and delete. You can see here https://github.com/Tushant/foodie-rest-api/tree/master/restaurants/api – Serenity Sep 04 '16 at 15:31
  • If you follow the DFW tutorial, you should use one serializer class and put your custom field there. – Windsooon Sep 04 '16 at 15:46
  • DFW tutorial? Sorry I didn't get you. – Serenity Sep 04 '16 at 16:03