I am trying to return an HttpResponse from the Django Rest Framework, including data from 2 related models. Models:
class Wine(models.Model): color = models.CharField(max_length=100, blank=True) country = models.CharField(max_length=100, blank=True) region = models.CharField(max_length=100, blank=True) appellation = models.CharField(max_length=100, blank=True) class Bottle(models.Model): wine = models.ForeignKey(Wine, null=False) user = models.ForeignKey(User, null=False, related_name='bottles')
I would like to have a serializer for the Bottle model that includes information from the associated Wine.
I tried:
class BottleSerializer(serializers.HyperlinkedModelSerializer): wine = serializers.RelatedField(source='wine') class Meta: model = Bottle fields = ('url', 'wine.color', 'wine.country', 'user', 'date_rated', 'rating', 'comment', 'get_more')
which does not work.
Any ideas how I could do this?
Thank:)
python django serialization django-rest-framework foreign-keys
bpipat Dec 17 '13 at 11:48 2013-12-17 11:48
source share