Comments (1)
The FlatMultipleModelAPIView
and ObjectMultipleModelAPIView
, out of the box, only support GET requests. In a large part this is because, since the whole point of the multiple model API views is to serialize multiple object types simultaneous, it's ambiguous as to what a POST or a PUT would mean in this context. If your /animals/
view had both a "Dog" and a "Cat", how would it determine where your POST was supposed to make a new Dog, Cat, or both?
If you wanted to add that functionality, and had a very clear idea of what you'd want the semantics to be, you could create your own view using one of the multiple model mixins and rest_framework's own builtin mixins, such as:
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import CreateModelMixin
from drf_multiple_model.mixins import ObjectMultipleModelMixin
from animals.models import Dog, Cat
from animals.serializers import DogSerializer, CatSerializer
class AnimalPostAndGetView(CreateModelMixin, ObjectMultipleModelMixin, GenericAPIView):
# the `querylist` field will be used during GET requests
querylist = [
{'queryset': Dog.objects.all(), 'serializer': DogSerializer},
{'queryset': Cat.objects.all(), 'serializer': CatSerializer},
]
# the `queryset` and `serializer_class` fields will be used for POST request to create a `Dog`
queryset = Dog.objects.all()
serializer_class = DogSerializer
So that would definitely work for using GET to get multiple models and using POST for creating a Dog object, although it's a little awkward to have both the queryset AND querylist attributes, but they are required for the different interfaces for the different mixins. If, on the other hand, if you wanted create something that could POST to create either a Dog or a Cat, you'd have to create that logic for yourself.
from djangorestmultiplemodels.
Related Issues (20)
- Is there a way to add post method? HOT 1
- AssertionError with ViewSets HOT 5
- Paginations HOT 1
- Different sorting fields based on query paramater HOT 4
- django-filter support HOT 6
- Pagination question HOT 2
- Viewsets doc is outdated
- Outdated document content HOT 2
- Is there a way to mix single object and list ?
- Is it possible to send context with the querylist? HOT 1
- AssertionError: 'ViewSet' should either include a `serializer_class` attribute, or override the `get_serializer_class()` method. HOT 1
- GeoFeatureModelSerializer HOT 1
- Can I using it in modelviewset? HOT 1
- Sorting by a date field HOT 1
- The Viewsets could support POST, PUT, PATCH, etc. HOT 2
- AssertionError: DataViewSet.get_queryset() returned None HOT 3
- Receiving attribute error viewset has no attribute 'get_extra_actions' HOT 1
- Query List only returns objects of one model
- Sorting by date + pagination
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from djangorestmultiplemodels.