[英]DoesNotExist while creating new object - tastypie
models.py
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
资源中添加了ModelType
的ForeignKey
关系。 并且ModelType
的值在ModelType
save方法中BaseModel
。 我无法找出错误的确切位置。
在您的情况下,您将Patient
创建为BaseModel
的子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
表),因为表方案已被替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.