2014-01-29 39 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