2014-01-29 38 views
0

models.pyDoesNotExist同時創造新的對象 - tastypie

class ModelType(models.Model): 
    Name = models.CharField("Name", max_length=50) 

    def __unicode__(self): 
     return unicode(self.Name) 


class BaseModel(models.Model): 
    ModelType = models.ForeignKey(ModelType) 
    Created = models.DateTimeField() 
    Modified = models.DateTimeField() 

    def __unicode__(self): 
     return unicode(self.id) 

    def save(self, *args, **kwargs): 
     print 'inside basemodel save' 

     if self.ModelType==None: 
      try: 
       self.ModelType = ModelType.objects.get(Name=self.__class__.__name__) 
      except: 
       m = ModelType(Name=self.__class__.__name__) 
       m.save() 
       self.ModelType = m 
     if self.id in [None, '']: 
      self.Created = datetime.datetime.now() 
     self.Modified = datetime.datetime.now() 

     print self.ModelType 
     super(BaseModel, self).save(*args, **kwargs) 

class Patient(BaseModel): 
    Name = models.CharField('Name', max_length=100) 

resource.py

class ModelTypeResource(ModelResource): 
    class Meta: 
     queryset = ModelType.objects.all() 
     filtering = {"Modified": ALL} 
     authorization = Authorization() 
     always_return_data = True 


class BaseModelResource(ModelResource): 
    ModelType = fields.ForeignKey(ModelTypeResource, 'ModelType', full=False, null=True) 
    class Meta: 
     queryset = BaseModel.objects.all() 
     filtering = {"Modified": ALL, 'ClinicDevice': ALL, 'ModelType': ALL} 
     authorization = Authorization() 
     always_return_data = True 


class PatientResource(ModelResource): 
    ModelType = fields.ForeignKey(ModelTypeResource, 'ModelType', full=False, null=True) 

    class Meta: 
     queryset = Patient.objects.all() 
     filtering = {"Modified": ALL} 
     authorization = Authorization() 
     always_return_data = True 

現在,如果我執行下面的命令來添加Patient

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"Name":"p1"}' http://localhost:8001/api/v1/patient/ 

以下錯誤被拋出

HTTP/1.0 404 NOT FOUND 
Date: Wed, 29 Jan 2014 14:10:59 GMT 
Server: WSGIServer/0.1 Python/2.7.3 
Content-Type: application/json 

{ 
    "error_message": "", 
    "traceback": " 

Traceback (most recent call last): 

    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 195, in wrapper 
    response = callback(request, *args, **kwargs) 

    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 426, in dispatch_list 
    return self.dispatch('list', request, **kwargs) 

    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 458, in dispatch 
    response = method(request, **kwargs) 

    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 1320, in post_list 
    updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs)) 

    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 2083, in obj_create 
    bundle = self.full_hydrate(bundle) 

    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 876, in full_hydrate 
    value = field_object.hydrate(bundle) 

    File "/usr/local/lib/python2.7/dist-packages/tastypie/fields.py", line 735, in hydrate 
    value = super(ToOneField, self).hydrate(bundle) 

    File "/usr/local/lib/python2.7/dist-packages/tastypie/fields.py", line 166, in hydrate 
    elif self.attribute and getattr(bundle.obj, self.attribute, None): 

    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 389, in __get__ 
    raise self.field.rel.to.DoesNotExist 

DoesNotExist 


"} 

我在Patient的資源中添加了ForeignKey關係ModelType。並且ModelType的值在BaseModel的保存方法內設置。我無法弄清楚錯誤的具體位置。

回答

0

就你而言,你創建了Patient作爲BaseModel的子項。在Django中,沒有真正的inherited模型類。改爲創建一個指針BaseModel

如果您不需要BaseModel的存在,請確保您有abstract = True

class BaseModel(models.Model): 
    ModelType = models.ForeignKey(ModelType) 
    Created = models.DateTimeField() 
    Modified = models.DateTimeField() 
    class Meta: 
     abstract = True 
    # ... the rest of your code 

之後,因爲表方案已經更換,不要忘記重新創建您的數據庫(或者,至少Patient表)。

+0

感謝您的建議。但是我需要'BaseModelResource',因此需要'BaseModel'的存在。 –

+0

如果是這樣,不要讓從'BaseModel'繼承的'Patient'模型,而是在'Patient'內創建一個指向'BaseModel'的外鍵字段。它更安全,更具可讀性。 – adityasdarma1