Multiple models in Django Rest Framework?


Coderaemon:

I am using Django Rest Framework . I want to serialize multiple models and send as response. Currently, only one model can be sent per view (as CartViewshown below, only Cart objects are sent). The following models (unrelated) can be there.

class Ship_address(models.Model):
   ...

class Bill_address(models.Model):
   ...

class Cart(models.Model):
   ...

class Giftwrap(models.Model):
   ...

I tried using DjangoRestMultiplemodels and it works fine with some limitations. Is there a built-in method? Can't I append to the serializer created in the following view?

from rest_framework.views import APIView

class CartView(APIView):
    """
    Returns the Details of the cart
    """

    def get(self, request, format=None, **kwargs):
        cart = get_cart(request)
        serializer = CartSerializer(cart)
        # Can't I append anything to serializer class like below ??
        # serializer.append(anotherserialzed_object) ??
        return Response(serializer.data)

I really like DRF. But this use case (sending multiple objects) makes me wonder if writing a simple old Django view would be better suited for this need.

grocery store:

You can customize it, and it wouldn't be too weird since this is a APIView(as opposed to where ModelViewSethumans expect a GET to return a single model ), e.g. you can return multiple objects from different models in a GET response

def get(self, request, format=None, **kwargs):
    cart = get_cart(request)
    cart_serializer = CartSerializer(cart)
    another_serializer = AnotherSerializer(another_object)

    return Response({
        'cart': cart_serializer.data,
        'another': another_serializer.data,
        'yet_another_field': 'yet another value',
    })

Related


Multiple models in Django Rest Framework?

Coderaemon: I am using Django Rest Framework . I want to serialize multiple models and send as response. Currently, only one model can be sent per view (as CartViewshown below, only Cart objects are sent). The following models (unrelated) can be there. class S

Multiple models in Django Rest Framework?

Coderaemon I am using Django Rest Framework . I want to serialize multiple models and send as response. Currently I can only send one model per view (as CartViewbelow, only the Cart object). The following models (unrelated) can be there. class Ship_address(mod

Multiple models in Django Rest Framework?

Coderaemon: I am using Django Rest Framework . I want to serialize multiple models and send as response. Currently, only one model can be sent per view (as CartViewshown below, only Cart objects are sent). The following models (unrelated) can be there. class S

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Django Rest Framework, querying with multiple models

Ben2pop I'm very new to using REST frameworks and I didn't find in tutorials how to achieve what I'm looking for; In my serialiser.py I have a class for serializing MyUser model class MyUserSerializer(ModelSerializer): class Meta: model = MyUser

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Search tables for multiple models in Django Rest Framework

DjangoNoob I have 3 tables PC(ID, PcNAME, Brand) , CellPhoness(ID, CellPhoneName, Brand) , Printers(ID, PrinterName, Brand) . There is no relationship between these three tables. I want to run a query where the user can enter a search string and the program wi

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Django-Rest Framework multiple models

collet I'm starting to use Django Rest Framework and it's a great tool! Actually, I'm stuck in some simple puzzles, but have no way of figuring out how to do it...I have two models, CustomUser and Order. Here, CustomUser has 0 to many orders. I want to generat

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Search tables for multiple models in Django Rest Framework

DjangoNoob I have 3 tables PC(ID, PcNAME, Brand) , CellPhoness(ID, CellPhoneName, Brand) , Printers(ID, PrinterName, Brand) . There is no relationship between these three tables. I want to run a query where the user can enter a search string and the program wi

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Django Rest Framework: No Models

Facebook facebook Rahul Nama to contact I am creating a REST API using DRF (Django Rest Framework). The API just takes the user's twitter handle and returns their twitter data. Here, I'm not using a model here because it's not required. Should I be using a ser

Django Rest Framework: No Models

Facebook facebook Rahul Nama to contact I am creating a REST API using DRF (Django Rest Framework). The API just takes the user's twitter handle and returns their twitter data. Here, I'm not using a model here because it's not required. Should I be using a ser

Return results from multiple models using Django REST Framework

aendrew : I have three models - Article, Author and Tweet. I eventually need to use the Django REST Framework to build a feed that aggregates all the objects into a reverse temporal feed using the Article and Tweet models. Any idea what I would do? I feel like

Return results from multiple models using Django REST Framework

Arndru I have three models - Article, Author and Tweet. I eventually need to construct a feed using Django REST Framework that aggregates all objects using the Article and Tweet models into one reverse chronological feed. Any idea what I would do? I feel like

Return results from multiple models using Django REST Framework

aendrew : I have three models - Article, Author and Tweet. I eventually need to use Django REST Framework to build a feed that aggregates all objects into a reverse temporal feed using Article and Tweet models. Any idea what I would do? I feel like I need to c

Return results from multiple models using Django REST Framework

aendrew : I have three models - Article, Author and Tweet. I eventually need to use Django REST Framework to build a feed that aggregates all objects into a reverse temporal feed using Article and Tweet models. Any idea what I would do? I feel like I need to c

Django rest framework. Returns nested multiple models

tomoc4 I'm trying to create a combined viewset that displays data in a nested format of three nested models. I got an error when trying to return to the viewset. Got AttributeError when attempting to get a value for field `runner` on serializer `CombinedSerial

Return results from multiple models using Django REST Framework

Arndru I have three models - Article, Author and Tweet. I eventually need to construct a feed using Django REST Framework that aggregates all objects using the Article and Tweet models into one reverse chronological feed. Any idea what I would do? I feel like

Django rest framework. Returns nested multiple models

tomoc4 I'm trying to create a combined viewset that displays data in a nested format of three nested models. I got an error when trying to return to the viewset. Got AttributeError when attempting to get a value for field `runner` on serializer `CombinedSerial