在创建新对象时不会出现 - tastypie

时间:2014-01-29 14:21:15

标签: python django foreign-keys 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


"}

我在ForeignKey的资源中添加了ModelType的{​​{1}}关系。并且Patient的值在ModelType的保存方法中设置。我无法确定错误的确切位置。

1 个答案:

答案 0 :(得分: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表),因为表格方案已被替换。