Giter Club home page Giter Club logo

Comments (1)

MattBroach avatar MattBroach commented on June 29, 2024 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.