当我通过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"
请任何人帮我解决此错误。
答案 0 :(得分:0)
它提供"environments_environment.client_id may not be NULL"
,因为
client = models.ForeignKey(Client)
默认值为null = False。所以你应该设置客户端ID。或者如果你不想这样做,
client = models.ForeignKey(Client, null=True, blank=True)