Python tastypie-模型'';具有空属性
当使用fields.ToManyField()执行反向关系时,我在单击Python tastypie-模型'';具有空属性,python,django,tastypie,Python,Django,Tastypie,当使用fields.ToManyField()执行反向关系时,我在单击 http://localhost:8080/api/v1/environment_test_device/?format=json 我看到了 The model '' has an empty attribute 'device_type_set' and doesn't allow a null value. 我想知道为什么模型是“”。我猜我的api路径设置不正确。是否有方法验证文件路径是否正确,或者为什么会出现此错误
http://localhost:8080/api/v1/environment_test_device/?format=json
我看到了
The model '' has an empty attribute 'device_type_set' and doesn't allow a null value.
我想知道为什么模型是“”。我猜我的api路径设置不正确。是否有方法验证文件路径是否正确,或者为什么会出现此错误?我真的不明白“设备类型集”应该叫什么。我正在查看,但“author_set”实际上与给定示例中的任何内容都不对应
我的模型是
class DeviceType(models.Model):
device_type_id = models.AutoField(primary_key=True)
device_name = models.CharField(max_length=255, unique=True)
class Meta:
db_table = u'device_type'
class Environment(models.Model):
environment_id = models.AutoField(primary_key=True)
environment_name = models.CharField(max_length=255, unique=True)
class Meta:
db_table = u'environment'
class Test(models.Model):
test_id = models.AutoField(primary_key=True)
test_name = models.CharField(max_length=255, unique=True)
class Meta:
db_table = u'test'
class EnvironmentTestDevice(models.Model):
environment_test_device_id = models.IntegerField(primary_key=True)
environment = models.ForeignKey(Environment)
test = models.ForeignKey(Test)
device_type = models.ForeignKey(DeviceType)
class Meta:
db_table = u'environment_test_device'
我的api.py是
class DeviceTypeResource(ModelResource):
environment_test_device = fields.ToManyField('resources.EnvironmentTestDeviceResource', 'device_type_set', related_name = 'device_type', full=True)
class Meta:
queryset = DeviceType.objects.all()
authorization = Authorization()
resource_name = 'device_type'
class EnvironmentResource(ModelResource):
class Meta:
queryset = Environment.objects.all()
authorization = Authorization()
resource_name = 'environment'
class TestResource(ModelResource):
class Meta:
queryset = Test.objects.all()
authorization = Authorization()
resource_name = 'test'
class EnvironmentTestDeviceResource(ModelResource):
device_type = fields.ForeignKey(DeviceTypeResource, 'device_type', full=True, null=True)
test = fields.ForeignKey(TestResource, 'test', full=True, null=True)
environment = fields.ForeignKey(EnvironmentResource, 'environment', full=True, null=True)
class Meta:
queryset = PipelineTestDevice.objects.all()
authorization = Authorization()
resource_name = 'environment_test_device'
在下一行的旁注上
environment_test_device = fields.ToManyField('resources.EnvironmentTestDeviceResource', 'device_type_set', related_name = 'device_type', full=True)
我已经把它注释掉了,我能够得到正确的json响应,但我假设我需要反向关系。我还尝试将“resources.EnvironmentTestDeviceResource”设置为
“project.api.resources.EnvironmentTestDeviceResource”和“project.api.EnvironmentTestDeviceResource”并收到相同的错误响应。”project'是我的应用程序的名称我认为问题在于属性的名称。使用
设备类型设置
代替设备类型设置
。但您应该在Django shell中进行测试。这里的path.to.api应该是什么?
environment_test_device = fields.ToManyField('EnvironmentTestDeviceResource', 'devicetype_set', related_name = 'device_type', full=True)