django-rest-viewsets

Call view from another view

我怕爱的太早我们不能终老 提交于 2021-02-20 04:07:45
问题 I have a viewset with one of the views as: @list_route(methods=["get"], url_path="special") def special(): pass And I call this view from another view like: view_fn = viewset.as_view({'get': 'list'}) response = view_fn(request) But it does not call my special function which maps to "/special/" , instead it calls the function which maps to "/" . I guess I need to pass url_path somehow or get the view using view name ? However, I'm not sure how to do either. 回答1: This will not work because you

'collections.OrderedDict' object has no attribute 'pk' - django rest framework

北战南征 提交于 2021-02-07 19:10:37
问题 I have a model and I want to write an update() method for it in order to update. The below snippet is my model: class Klass(models.Model): title = models.CharField(max_length=50) description = models.CharField(max_length=500) university = models.CharField(max_length=50,blank=True, null=True) teacher = models.ForeignKey(Profile, related_name='teacher', on_delete=models.CASCADE) and the below snippet is corresponding Serializer : class KlassSerializer(ModelSerializer): teacher =

'collections.OrderedDict' object has no attribute 'pk' - django rest framework

旧巷老猫 提交于 2021-02-07 19:09:24
问题 I have a model and I want to write an update() method for it in order to update. The below snippet is my model: class Klass(models.Model): title = models.CharField(max_length=50) description = models.CharField(max_length=500) university = models.CharField(max_length=50,blank=True, null=True) teacher = models.ForeignKey(Profile, related_name='teacher', on_delete=models.CASCADE) and the below snippet is corresponding Serializer : class KlassSerializer(ModelSerializer): teacher =

'CityListViewSet' should either include a `serializer_class` attribute, or override the `get_serializer_class()` method

我与影子孤独终老i 提交于 2021-01-27 04:29:06
问题 I am assuming by the error in the title, once more here for clarity 'CityListViewSet' should either include a `serializer_class` attribute, or override the `get_serializer_class()` method. that my serializer isn't connected to my view, which in my code it should be. I'm not really sure where the bug is in this one. I wonder if any of you have seen something similar? Here is the code. Router: router.register(r'city-list', CityListViewSet, base_name='city-list') view: class CityListViewSet

How to implement Redis Cache with Django Rest Framework?

我们两清 提交于 2020-12-15 08:00:34
问题 I need to implement Redis cache with my Django Rest Framework site. But when I do load test using cache_page decorator with a class it improves the request per second but an error occurs "'function' object has no attribute 'get_extra_actions'" Views.py @cache_page(CACHE_TTL) class ParameterViewSet(viewsets.ModelViewSet): """ Lists all the parameters present in the system. Can pass filter with parent set to null to get top level Parameters. """ permission_classes = (IsAuthenticated,) queryset

Uploading multiple images and nested json using multipart/form-data in Django REST Framework

ε祈祈猫儿з 提交于 2020-04-28 11:02:14
问题 I have a problem with parsing request.data in viewset. I have a model that can add multiple images depending on a product. I want to split the image from the incoming data, send product data to ProductSerializer, and after that send image to its serializer with product data and save it. I have two model, simply like this: def Product(models.Model): name = models.CharField(max_length=20) color = models.ForeignKey(Color, on_delete=models.CASCADE) def Color(models.Model): name = models.CharField

Uploading multiple images and nested json using multipart/form-data in Django REST Framework

微笑、不失礼 提交于 2020-04-28 11:02:11
问题 I have a problem with parsing request.data in viewset. I have a model that can add multiple images depending on a product. I want to split the image from the incoming data, send product data to ProductSerializer, and after that send image to its serializer with product data and save it. I have two model, simply like this: def Product(models.Model): name = models.CharField(max_length=20) color = models.ForeignKey(Color, on_delete=models.CASCADE) def Color(models.Model): name = models.CharField

Use serializer of model having foreign key to do CRUD on parent table in Django Rest Framework

走远了吗. 提交于 2019-12-22 13:02:52
问题 In my API, I have two models Question and Option as shown below class Question(models.Model): body = models.TextField() class Options(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) option = models.CharField(max_length=100) is_correct = models.SmallIntegerField() While creating a question it would be nicer the options can be created at the same time. And already existed question should not be created but the options can be changed if the options are different