当我通过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 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"请任何人能帮助我解决这个错误。
发布于 2014-06-18 19:27:11
它提供了"environments_environment.client_id may not be NULL",因为
client = models.ForeignKey(Client)它采用默认的null=False.So,您应该设置客户端id。或者如果你不想这么做
client = models.ForeignKey(Client, null=True, blank=True)https://stackoverflow.com/questions/24283958
复制相似问题