So I have follwoing models:
class A(models.Model):
name = models.CharField()
age = models.SmallIntergerField()
class B(models.Model):
a = models.OneToOneF
I know this is an old post but I came across this and after some research and reading through the Django Rest Framework documentation So a quick search I found that you could use the "related_name" parameter for reverse relationships as stated here
reverse relationships are not automatically included by the ModelSerializer
and HyperlinkedModelSerializer
classes. To include a reverse relationship, you must explicitly add it to the fields list.
For example:
class AlbumSerializer(serializers.ModelSerializer):
class Meta:
fields = ['tracks', ...]
You'll normally want to ensure that you've set an appropriate related_name
argument on the relationship, that you can use as the field name.
For example:
class Track(models.Model):
album = models.ForeignKey(Album, related_name='tracks',
on_delete=models.CASCADE)
...
If you have not set a related name for the reverse relationship, you'll need to use the automatically generated related name
in the fields
argument.
For example:
class AlbumSerializer(serializers.ModelSerializer):
class Meta:
fields = ['track_set', ...]
Also, see the Django documentation on reverse relationships for more details.