django-tastypie:将 ModelResource 链接到资源
我目前正在尝试 django-tastypie 设计一个 RESTful api。我面临一个问题:
# the RevisionObject retrieve commits info through pysvn
# This Resource is fully functionnal (RevisionObject code is not here)
class RevisionResource(Resource):
id = fields.CharField(attribute='revision')
description = fields.CharField(attribute='message')
author = fields.CharField(attribute='author')
changed_path = fields.ListField(attribute='changed_paths')
class Meta:
object_class = RevisionObject
allowed_methods = ['get']
resource_name = 'revision'
class RevisionToApplyResource(ModelResource):
#### here's the problem
revision = fields.ToManyField(RevisionResource, 'revision')
####
class Meta:
queryset = RevisionToApply.objects.all()
在我的 models.py 中,我有:
class RevisionToApply(models.Model):
patch = models.ForeignKey(PatchRequest)
revision = models.PositiveIntegerField()
applied = models.BooleanField(default = False)
我的问题是 RevisionToApply 模型(对于 django)使用 int 进行修订。
我如何告诉 tastypie 使用 RevisionToApplyResource 的修订字段作为指向 RevisionResource 的指针?如果 ToXxxxField 仅用于与 django 模型链接,那么插入 ResourceObject 的最佳时机是什么?
谢谢。
class NoForeignKeyToOneField(ToOneField):
def dehydrate(self, bundle):
try:
obj_key = getattr(bundle.obj, self.attribute)
foreign_obj = self.to_class().obj_get(pk=obj_key)
except ObjectDoesNotExist:
foreign_obj= None
if not foreign_obj:
if not self.null:
raise ApiFieldError("The model '%r' has an empty attribute"
"'%s' and doesn't allow null value." % (bundle.obj,
self.attribute))
return None
self.fk_resource = self.get_related_resource(foreign_obj)
fk_bundle = Bundle(obj=foreign_obj, request=bundle.request)
return self.dehydrate_related(fk_bundle, self.fk_resource)
I'm currently trying django-tastypie to design a RESTful api. I'm facing a problem:
# the RevisionObject retrieve commits info through pysvn
# This Resource is fully functionnal (RevisionObject code is not here)
class RevisionResource(Resource):
id = fields.CharField(attribute='revision')
description = fields.CharField(attribute='message')
author = fields.CharField(attribute='author')
changed_path = fields.ListField(attribute='changed_paths')
class Meta:
object_class = RevisionObject
allowed_methods = ['get']
resource_name = 'revision'
class RevisionToApplyResource(ModelResource):
#### here's the problem
revision = fields.ToManyField(RevisionResource, 'revision')
####
class Meta:
queryset = RevisionToApply.objects.all()
In my models.py I have:
class RevisionToApply(models.Model):
patch = models.ForeignKey(PatchRequest)
revision = models.PositiveIntegerField()
applied = models.BooleanField(default = False)
My problem is that the RevisionToApply models (for django) uses an int to the revision.
How can I tell tastypie to use the revision field of RevisionToApplyResource as a pointer to a RevisionResource? If the ToXxxxField are only for linking with django models, what is the perfect moment to insert the ResourceObject?
thanks.
class NoForeignKeyToOneField(ToOneField):
def dehydrate(self, bundle):
try:
obj_key = getattr(bundle.obj, self.attribute)
foreign_obj = self.to_class().obj_get(pk=obj_key)
except ObjectDoesNotExist:
foreign_obj= None
if not foreign_obj:
if not self.null:
raise ApiFieldError("The model '%r' has an empty attribute"
"'%s' and doesn't allow null value." % (bundle.obj,
self.attribute))
return None
self.fk_resource = self.get_related_resource(foreign_obj)
fk_bundle = Bundle(obj=foreign_obj, request=bundle.request)
return self.dehydrate_related(fk_bundle, self.fk_resource)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我就是这样做的。看一下
ToOneField
类的工作原理,您会注意到水合/脱水方法对负责获取和设置实际的相关实例。通过子类化ToOneField
并重写这两个方法,您无需实际外键即可获得 Tastypie 的自动化资源处理的优势。(我指的是
ToOneField
而不是ToManyField
,因为在您的模型中,给定的RevisionToApply
似乎只能指向一个修订版。)它看起来像这样:
然后在资源中使用此字段类型,而不是基本的
ToOneField
。我还没有测试过它,但我相信这个方法是合理的、简单的,并且它会完成工作。Here's how I would do it. Taking a look at how the
ToOneField
class works, you'll notice that the hydrate/dehydrate method pair takes care of getting and setting the actual related instance. By subclassingToOneField
and overriding these two methods, you can get the benefit of Tastypie's automated resource handling without an actual foreign key.(I'm referring to
ToOneField
rather thanToManyField
because in your model, a givenRevisionToApply
can only point to one revision, it seems.)It would look something like this:
And then use this field type in your resource rather than the basic
ToOneField
. I haven't tested it , but I believe the approach is sound, simple and it'll get the job done.