Python &引用;错误消息:&引用;环境“u environment.client”id不能为空;
当我通过curl创建数据时,我得到了以下错误Python &引用;错误消息:&引用;环境“u environment.client”id不能为空;,python,django,tastypie,Python,Django,Tastypie,当我通过curl创建数据时,我得到了以下错误 "error_message": "environments_environment.client_id may not be NULL" 我的django模型是 class Client(models.Model): name = models.CharField(max_length=100) def __unicode__(self): return u'%s' % self.name class Enviro
"error_message": "environments_environment.client_id may not be NULL"
我的django模型是
class Client(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return u'%s' % self.name
class Environment(models.Model):
client = models.ForeignKey(Client)
name = models.CharField(max_length=100)
def __unicode__(self):
return u'%s:%s' % (self.client.name, self.name)
我的资源是
class ClientResource(ModelResource):
class Meta:
queryset = Client.objects.all()
resource_name = 'clients'
allowed_methods = ['get', 'put', 'delete', 'post']
authorization = Authorization()
class EnvironmentResource(ModelResource):
client = fields.ForeignKey(ClientResource, 'clients', full=True, null=True, blank=True)
class Meta:
queryset = Environment.objects.all()
resource_name = 'environments'
allowed_methods = ['get', 'put', 'delete', 'post']
authorization = Authorization()
创建客户端后,我尝试通过
curl -X POST --data '{"client":"/api/v1/clients/1/","name":"rpcenv"}' \
-H 'Authorization: xxxxxxxx' \
-H 'Content-Type: application/json;charset=UTF-8' \
http://example.com/api/v1/environments/
但输出结果似乎是
"error_message": "environments_environment.client_id may not be NULL"
请任何人帮我解决这个错误。它给出了“environments\u environment.client\u id不能为NULL”
,因为
client = models.ForeignKey(Client)
它采用默认的null=False。因此您应该设置客户端id。或者,如果您不想这样做
client = models.ForeignKey(Client, null=True, blank=True)
是否可以在tastype级别修复此问题,如client=fields.ForeignKey(ClientResource,'clients',full=True,null=True,blank=True)