Question

I have a table with few foreign keys in it and I'm trying to get the nested resources however the resources are returning null ie when making the following call

http://localhost:8080/api/v1/testuns/?format=json

I get back

{
meta: {
    limit: 20,
    next: null,
    offset: 0,
    previous: null,
    total_count: 1
},
objects: [
     {
       phone: {
           devices: null,
           modelnumber: null,
           phone_id: "2",
           resource_uri: "/api/v1/phone/2/"
       },
       resource_uri: "/api/v1/testuns/0/",
       result: "PASS",
       score: 90,
       test_run_id: "0"
     }
   ]
}

As you can see my devices and modelnumber does not have the nested resources

My api.py is as such

class DevicesResource(ModelResource):
    class Meta:
        queryset = Devices.objects.all()
        authorization = Authorization()
        resource_name = 'devices'

class ModelnumberResource(ModelResource):
    class Meta:
        queryset = Modelnumber.objects.all()
        authorization = Authorization()
        resource_name = 'modelnumber'

class PhoneResource(ModelResource):
    devices = fields.ForeignKey(DevicesResource, 'devices', full=True, null=True)
    modelnumber = fields.ForeignKey(ModelnumberResource, 'modelnumber', full=True, null=True)

    class Meta:
        queryset = Phone.objects.all()
        authorization = Authorization()
        resource_name = 'phone'

class TestunsResource(ModelResource):
    phone = fields.ForeignKey(PhoneResource, 'phone', full=True, null=True)
    class Meta:
        queryset = Testuns.objects.all()
        authorization = Authorization()
        resource_name = 'testuns'

and my models.py is

class Devices(models.Model):
    device_id = models.AutoField(primary_key=True)
    device_name = models.CharField(max_length=135, unique=True, blank=True)
    class Meta:
        db_table = u'devices'

class Modelnumber(models.Model):
    model_number_id = models.AutoField(primary_key=True)
    model_name = models.CharField(max_length=135, unique=True, blank=True)
    class Meta:
        db_table = u'modelnumber'

class Phone(models.Model):
    phone_id = models.AutoField(primary_key=True)
    model_number = models.ForeignKey(Modelnumber)
    device = models.ForeignKey(Devices)
    class Meta:
        db_table = u'phone'

class Testuns(models.Model):
    test_run_id = models.AutoField(primary_key=True)
    score = models.IntegerField()
    phone = models.ForeignKey(Phone)
    result = models.CharField(max_length=135)

Why are some of my nested resource returning null?

UPDATE: Adding mysql queries

mysql> select * from testuns;
+-------------+-------+----------+--------+
| test_run_id | score | phone_id | result |
+-------------+-------+----------+--------+
|           0 |    90 |        2 | PASS   |
+-------------+-------+----------+--------+
1 row in set (0.00 sec)

mysql> select * from phone;
+----------+-----------------+-----------+
| phone_id | model_number_id | device_id |
+----------+-----------------+-----------+
|        2 |               1 |         1 |
+----------+-----------------+-----------+
1 row in set (0.00 sec)

mysql>
Was it helpful?

Solution

Accidentally your comment pointed me out on this solution:

class PhoneResource(ModelResource):
    device = fields.ForeignKey(DevicesResource, 'device', full=True, null=True)
    model_number = fields.ForeignKey(ModelnumberResource, 'model_number', full=True, null=True)

    class Meta:
        queryset = Phone.objects.all()
        authorization = Authorization()
        resource_name = 'phone'

Name your resource fields exactly as they are named in model.

I tested it on my own project and when the field name in resource is different then in model it returns null.

The problem was misleading because of null=True and blank=True presence. Which makes it working correctly but not according your plan. I couldn't find explenation in docs but found something in docstrings:

The ``attribute`` argument should specify what field/callable points to the related data on the instance object. Required. docstring

EDIT:

Here line of code your field isn't foreign key but it can be null that's why it did not raised any exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top