Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRF - Nested Serialization Of m2m fields with through model

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!

like image 212
MrName Avatar asked Mar 09 '23 00:03

MrName


1 Answers

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       
    )
like image 74
MrName Avatar answered Apr 26 '23 09:04

MrName