Heyo! 

I've been using DRF for quite some time, so I'd like to start by saying: 
thanks so much! It's been very useful to me and I appreciate it sincerely.

Anyway, I'm trying to tie a few things together:

class Leaderboard(models.Model):
    competition = models.ForeignKey('competitions.Competition', 
on_delete=models.CASCADE, related_name="leaderboards")


class Column(models.Model):
    leaderboard = models.ForeignKey(Leaderboard, on_delete=models.CASCADE, 
related_name="columns")
    shared_columns = models.ManyToManyField('Column')    


class Competition(models.Model):
    pass

So what I'd like to do is make sure any Column that is "shared_columns" 
with another belongs to the same Leaderboard. So you the "filtering" 
queryset would look something like...

class ColumnSerializer(WritableNestedModelSerializer):
    computation_columns = 
serializers.PrimaryKeyRelatedField(queryset=Column.objects.all(), 
many=True, required=False)

    class Meta:
        model = Column
        fields = (
            'shared_columns'
        )

    def get_fields(self):
        fields = super().get_fields()
        if self.instance:
            fields['computation_columns'].queryset = 
self.instance.leaderboard.columns.all()
        return fields

However, I can't seem to figure out how to *ever* get the instance of the 
parent leaderboard before `create()` or `update()`. I did find out about 
`self.root` to get the base instance of a Competition, but I have no idea 
how to associate that with a column underneath..

Thanks for any help!

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to