[Answer]-Retrieve related field detail instead of id

1👍

You can do that using natural keys. You need to define natural_key() method in Status model and pass use_natural_foreign_keys=True.

class Orders(models.Model):
   ....
   status = models.ForeignKey('Status')

class Status(models.Model):
    status_name = models.CharField(max_length=CHARFIELD_MAX_LEN)
    #def natural_key(self):
        return (self.status_name,)

Then to serialize objects

all_orders = Orders.objects.all()
resp = serializers.serialize('json', all_orders, use_natural_foreign_keys=True)
👤Rohan

0👍

according to here:

all_orders= Orders.objects.all().values_list('status__status_name')

It will get the list of status name

👤ruddra

Leave a comment