4

I am using a m2m field with a through model in DRF. Everything is working fine, EXCEPT when I try to nest the membership serializer.

models.py

class SweepStakes(models.Model):                                                          
    name = models.CharField(max_length=255)                                      

class Event(models.Model):                                                                       
    sweepstakes = models.ManyToManyField(SweepStakes, through='EventSweepStakesMembership')

class EventSweepStakesMembership(models.Model):                                  
    event = models.ForeignKey(Event, on_delete=models.CASCADE)                   
    sweepstakes = models.ForeignKey(SweepStakes, on_delete=models.CASCADE)       
    enabled = models.BooleanField(default=False)

serializers.py

class EventSweepStakesSerializer(serializers.ModelSerializer):                      
    name = serializers.ReadOnlyField(source='sweepstakes.name')                     

    class Meta:                                                                     
        model = EventSweepStakesMembership                                          
        fields = ('name', 'enabled',)

class EventSerializer(BaseTenantSerializer):                                                                     
    sweepstakes = EventSweepStakesSerializer(many=True, read_only=True)             

    class Meta:                                                                     
        model = Event                                                               
        fields = ('sweepstakes',)

At this point, if I hook the EventSweepStakesMembership model and the EventSweepStakesSerializer up to a view, I get back exactly what I expect, output like this:

{"name": "thingy", "enabled" true}

However, when I hook the Event model and EventSerializer serializer into a view, the sweepstakes field returns an empty dictionary instead of the nested representation, like so:

{"sweepstakes": [{}]}

Note that it is NOT an empty array, in other words it does see the related through model, but simply does not serialize it correctly when displaying.

There is no error, it is just empty. I have tried increasing the depth of the Event serializer to no avail.

Am I missing something or maybe even going about this all wrong?

Thanks!

MrName
  • 2,363
  • 17
  • 31

1 Answers1

9

Got it, thanks to this answer:

https://stackoverflow.com/a/17263583/1366989

The missing element here was the source kwarg on the EventSerializer. So, it now looks like this, and is working as expected:

class EventSerializer(BaseTenantSerializer):                                                                
    sweepstakes = EventSweepStakesSerializer(                                    
        source='eventsweepstakesmembership_set', many=True, read_only=True       
    )
MrName
  • 2,363
  • 17
  • 31
  • Thank you my friend. You quite possibly saved me a couple of hours which otherwise would have been spent digging into DRF source.. – Hari Mahadevan Feb 17 '20 at 14:50