2014-06-18 85 views
0

當我通過curl創建數據時,出現以下錯誤。「error_message」:「environments_environment.client_id可能不是NULL」

"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

它給"environments_environment.client_id may not be NULL"因爲

client = models.ForeignKey(Client) 

它採用默認爲空= False.So你應該設置客戶端ID。或者如果你不想這樣做,

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

是否有可能解決這個問題在tastypie級別,如client = fields.ForeignKey(ClientResource,'clients',full = True,null = True,blank =真正) – Selva

相關問題