Discussion:
Reverse Foreign key lookup
KP
2018-07-18 20:41:41 UTC
Permalink
Hello,

I have 2 models

class Suite(models.Model):
name = models.CharField(blank=False, null=False, max_length=100)



class Comments(models.Model):
suite = models.ForeignKey(Suite, on_delete=models.PROTECT)
comment = models.CharField(blank=False, null=False, max_length=100)

*Current Output for localhost:8000/suite/: *

"results": [
{
"id": 6,
"name": "nb",
}



*Desired Output:*

"results": [
{
"id": 6,
"comments": {
"id": 1,
"comment": "comment 23456",
},
{
"id": 2,
"comment": "comment 789",
},


"name": "nb",
}




I tried :

class SuiteSerializer(serializers.ModelSerializer):
comments = CommentsSerializer(many=True, read_only=True)

class Meta:
model = Suite
fields = ('name', 'comments')

Also
class SuiteList(ListView):
queryset = QuarantinedSuite.objects.all()
queryset.suite_set.all()



Can someone please help with this?
--
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 django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...