action methods is not allowed to have more methods rather than their viewsets

168 views
Skip to first unread message

Ali Yazdi

unread,
Sep 29, 2020, 11:16:23 AM9/29/20
to Django REST framework
1. I have a `viewsets.ModelViewSet` that has `http_method_names = ['get']`
2. and its action method needs to have `methods=['get', 'post']` 
3. But `post` method is not allowed for its action
4. I don't is it normal behavior to have this scenario or not?

Carl Nobile

unread,
Sep 29, 2020, 2:17:59 PM9/29/20
to django-res...@googlegroups.com
If you set `http_method_names = ['get']` and the POST is on the same endpoint it won't allow a POST. You might want to add 'post' to that variable.
I, myself don't use ViewSets. I tend to use generic Views. In your case the ListCreateAPIView generic view in this case. You can find it at https://www.django-rest-framework.org/api-guide/generic-views/#listcreateapiview

~Carl

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/abec1376-0619-4557-a7ea-1bab1f6473aan%40googlegroups.com.


--
-------------------------------------------------------------------------------
Carl J. Nobile (Software Engineer)
carl....@gmail.com
-------------------------------------------------------------------------------

Ali Yazdi

unread,
Sep 29, 2020, 3:34:35 PM9/29/20
to Django REST framework
Thanks for your answer. I try using `(mixins.RetrieveModelMixin,mixins.ListModelMixin, GenericViewSet)` without setting `http_method_names = ['get']` and my action works fine for `post` method.
Thank you Carl.

Ali Yazdi

unread,
Sep 29, 2020, 4:10:46 PM9/29/20
to Django REST framework
I've got a new problem with this new viewset that when I generate schema with `generateschema --file schema.yml` it says:
```
 UserWarning: <class 'recommendation.api.views.RecommendationViewSet'> is not compatible with schema generation
  warnings.warn(
```
But, it creates the schema of that viewset!

Carl Nobile

unread,
Sep 30, 2020, 11:06:13 AM9/30/20
to django-res...@googlegroups.com
As I said I don't use ViewSets, but if you add in mixins that cause the signature of the ViewSet to change it cannot easily and automatically generate schemas--at least this is my understanding.
The mixins are used in generic views and are not generally needed when defining your own views except under very few situations when you need to override a lot of functionality in the given generic views.
You might want to just use the generic view I mentioned previously.

ViewSets are there to make things easy and give you some additional perks, but they take away a lot of flexibility.

~Carl


Reply all
Reply to author
Forward
0 new messages