I'm using Django Rest Framework and have a model with multiple ForeignKey relationships and am POSTing their slugs rather than id. The issue is that the second ForeignKey requires a queryset that is filtered based on the first. Here is a not-so-perfect example:
# models.py
class Student(models.Model):
slug = models.SlugField(...)
class Class_(models.Model):
slug = models.SlugField(...)
class ReportCard(models.Model)
student = models.ForeignKey(Student)
class_ = models.ForeignKey(Class_)
grade = models.CharField(...)
# serializers.py
class ReportCardSerializer(serializers.ModelSerializer):
student = serializers.SlugRelatedField(queryset=models.Student.objects.all(), slug_field='slug')
class_ = serializers.SlugRelatedField(queryset=models.Class_.objects.filter(???), slug_field='slug') # need to filter based on student here
class Meta:
model = models.ReportCard
fields = '__all__' # use all fields
How do I do this? student in the serializer does not return the model and I don't know how to access the data inside of ReportCardSerializer to grab the string from the POST request to manually filter class_.