Discussion:
select_related with foreign keys
KP
2018-07-11 02:39:59 UTC
Permalink
Hello,

I have 2 models

class State(models.Model):
name = models.CharField(blank=False, null=False, max_length=100)
description = models.TextField(null=True)
class Meta:
db_table = 'state'


def __str__(self):
return self.name




class Suite(models.model):
user = models.CharField(blank=False, null=False, max_length=100)
state = models.ForeignKey(State, related_name='state', on_delete=models.
PROTECT)

class Meta:
db_table = 'suite'



Suite.objects.select_related() returns following output:
{
"type": "Suite",
"id": "1",
"attributes": {
"user": "nsupe",
},
"relationships": {
"state": {
"data": {
"type": "State",
"id": "1"
}
}
}
},


Is there a way to include *name* field from *state* table in the output:



{
"type": "Suite",
"id": "1",
"attributes": {
"user": "nsupe",
},
"relationships": {
"state": {
"data": {
"type": "State",
"id": "1"
"name": 'qua'
}
}
}
},




I tried Suite.objects.select_related('state'),
Suite.objects.all().select_related('state')
Suite.objects.select_related()
Suite.objects.all().prefetch_related('state').
Suite.objects.all().select_related('state', 'state_name')
--
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.
Jason
2018-07-11 10:33:52 UTC
Permalink
that has to be done in your serializer, check out the docs on nested
serializers

http://www.django-rest-framework.org/api-guide/relations/#example
Post by KP
Hello,
I have 2 models
name = models.CharField(blank=False, null=False, max_length=100)
description = models.TextField(null=True)
db_table = 'state'
return self.name
user = models.CharField(blank=False, null=False, max_length=100)
state = models.ForeignKey(State, related_name='state', on_delete=
models.PROTECT)
db_table = 'suite'
{
"type": "Suite",
"id": "1",
"attributes": {
"user": "nsupe",
},
"relationships": {
"state": {
"data": {
"type": "State",
"id": "1"
}
}
}
},
{
"type": "Suite",
"id": "1",
"attributes": {
"user": "nsupe",
},
"relationships": {
"state": {
"data": {
"type": "State",
"id": "1"
"name": 'qua'
}
}
}
},
I tried Suite.objects.select_related('state'),
Suite.objects.all().select_related('state')
Suite.objects.select_related()
Suite.objects.all().prefetch_related('state').
Suite.objects.all().select_related('state', 'state_name')
--
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.
Nirali Supe
2018-07-12 16:58:15 UTC
Permalink
Thank You Jason. It worked

On Jul 11, 2018 5:33 AM, "Jason" <***@gmail.com> wrote:

that has to be done in your serializer, check out the docs on nested
serializers

http://www.django-rest-framework.org/api-guide/relations/#example
Post by KP
Hello,
I have 2 models
name = models.CharField(blank=False, null=False, max_length=100)
description = models.TextField(null=True)
db_table = 'state'
return self.name
user = models.CharField(blank=False, null=False, max_length=100)
state = models.ForeignKey(State, related_name='state', on_delete=
models.PROTECT)
db_table = 'suite'
{
"type": "Suite",
"id": "1",
"attributes": {
"user": "nsupe",
},
"relationships": {
"state": {
"data": {
"type": "State",
"id": "1"
}
}
}
},
{
"type": "Suite",
"id": "1",
"attributes": {
"user": "nsupe",
},
"relationships": {
"state": {
"data": {
"type": "State",
"id": "1"
"name": 'qua'
}
}
}
},
I tried Suite.objects.select_related('state'),
Suite.objects.all().select_related('state')
Suite.objects.select_related()
Suite.objects.all().prefetch_related('state').
Suite.objects.all().select_related('state', 'state_name')
--
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.
--
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.
Continue reading on narkive:
Loading...