【问题标题】:"error_message": "environments_environment.client_id may not be NULL"“error_message”:“environments_environment.client_id 可能不为 NULL”
【发布时间】:2014-06-18 11:06:48
【问题描述】:

当我通过 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"

请谁能帮我解决这个错误。

【问题讨论】:

    标签: python django tastypie


    【解决方案1】:

    它给出"environments_environment.client_id may not be NULL" 因为

    client = models.ForeignKey(Client)
    

    它需要默认 null=False。所以你应该设置客户端 ID。或者如果你不想这样做,

    client = models.ForeignKey(Client, null=True, blank=True)
    

    【讨论】:

    • 是否可以在美味派级别解决此问题,例如 client = fields.ForeignKey(ClientResource, 'clients', full=True, null=True, blank=True)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 2023-01-18
    相关资源
    最近更新 更多