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 `CombinedSerializer`. The serializer field might be named incorrectly and not match any attribute or key on the `Runner` instance. Original exception text was: 'Runner' object has no attribute 'runner'.

serializers.py

class CombinedSerializer(serializers.Serializer):
    event = EventSerializer()
    market = MarketSerializer()
    runner = RunnerSerializer()
    class Meta:
        fields = ('event' , 'market', 'runner')

views.py

class CombinedViewSet(mixins.ListModelMixin, 
                      viewsets.GenericViewSet, 
                     mixins.RetrieveModelMixin):

    queryset = Runner.objects.all()
    serializer_class = CombinedSerializer

models.py

class Event(models.Model):
    sport_id = models.CharField(max_length=15)
    event_id = models.BigIntegerField(unique=True)
    event_name = models.CharField(max_length=200)
    start_time = models.DateTimeField(null=True)
    status = models.CharField(max_length=13)


class Market(models.Model):
    event = models.ForeignKey(Event, on_delete=models.CASCADE)
    market_id = models.BigIntegerField(unique=True)
    market_name = models.CharField(max_length=35)
    status = models.CharField(max_length=10)
    volume = models.FloatField(null=True)

class Runner(models.Model):
    event = models.ForeignKey(Event, null=True, default=None, on_delete=models.CASCADE)
    market = models.ForeignKey(Market, null=True, default=None, on_delete=models.CASCADE)
    runner_id = models.BigIntegerField(unique=True)
    name = models.CharField(max_length=500)
    back_odds = models.FloatField(null=True)
    lay_odds = models.FloatField(null=True)

For some reason, when I remove the runner from the serializer "events and markets" display in the api in nested format. When I add the Runner, I get the above error. How can I fix this and show all three in my API?

heme

Examples quesrysetinclude ViewSet: _Runner

queryset = Runner.objects.all()

But in it, CombinedSerializeryou've included the runnerfields (mapped to RunnerSerailizer) - you've already got a Runnermodel instance, so runnerthere are no fields there, only the fields available in the Runnermodel , any reverse-related fields, and you add on the serializer of custom fields.


You have two options:

Inherit ModelSerializerand include all fields:

class CombinedSerializer(serializers.ModelSerializer):
    event = EventSerializer()
    market = MarketSerializer()

    class Meta:
        model = Runner
        fields = '__all__'

Or, if you want more fine-grained control over the field, mention the field explicitly ( Serializerin which case you can keep the inheritance), eg:

class CombinedSerializer(serializers.Serializer):
    event = EventSerializer()
    market = MarketSerializer()

    class Meta:
        fields = ('event', 'market', 'name', 'back_odds')

If you want to inherit from serializers.ModelSerializer, you need to mention the model in the Metaclass :

class CombinedSerializer(serializers.ModelSerializer):
    event = EventSerializer()
    market = MarketSerializer()

    class Meta:
        model = Runner
        fields = ('event', 'market', 'name', 'back_odds')

Related


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

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

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

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

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 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 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 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 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

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, 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

Nested serializers "through models" in Django Rest Framework

bdex I'm having trouble serializing an intermediate "pivot" model and attaching it to each item in a "many-to-many" relationship in Django Rest Framework. example: models.py: class Member(models.Model): name = models.CharField(max_length = 20) groups =

Nested serializers "through models" in Django Rest Framework

bdex I'm having trouble serializing an intermediate "pivot" model and attaching it to each item in a "many-to-many" relationship in Django Rest Framework. example: models.py: class Member(models.Model): name = models.CharField(max_length = 20) groups =

Nested serializers "through models" in Django Rest Framework

bdex I'm having trouble serializing an intermediate "pivot" model and attaching it to each item in a "many-to-many" relationship in Django Rest Framework. example: models.py: class Member(models.Model): name = models.CharField(max_length = 20) groups =

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

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 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

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: 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

Django Rest Framework multiple nested writable serializers

Danny I'm trying to implement multiple nested writable serializers using django rest framework. I have read the documentation available http://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations I've been able to do this for on

Django Rest Framework multiple nested writable serializers

Danny I'm trying to implement multiple nested writable serializers using django rest framework. I have read the documentation available http://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations I've been able to do this for on

Django Rest Framework multiple nested writable serializers

Danny I'm trying to implement multiple nested writable serializers using django rest framework. I have read the documentation available http://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations I've been able to do this for on

Django Rest Framework multiple nested writable serializers

Danny I'm trying to implement multiple nested writable serializers using django rest framework. I have read the documentation available http://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations I've been able to do this for on