KP
2018-07-11 02:39:59 UTC
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')
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.
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.