如何结合多种风味的多种资源? [英] How to combine mutiple resources in django-tastypie?

查看:181
本文介绍了如何结合多种风味的多种资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有三个模型提交,联系和提交联系。

 类提交(models.Model):
title = models.CharField(max_length = 255,verbose_name ='Title')
...

class Contact(models.Model):
name = models.CharField max_length = 200,verbose_name ='Contact Name')
email = models.CharField(max_length = 80,verbose_name ='联系电子邮件')
...

class SubmissionContact

提交= models.ForeignKey(提交)
contact = models.Foreign(联系人,verbose_name ='联系人')
/ pre>

我可以使用单一的ModelResource使用tastypie来读/写所有这三个表。 (基本上获取和放置在tastypie中的列表和细节的操作)



提前感谢任何帮助。

解决方案

您可以将一个模型嵌套到另一个模型中,或者使用脱水循环向输出添加额外的资源,例如考虑一个Foo和Bar模型



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b b b b b b b b b b b b b b b b b b b b b b b b b b $ b serializer = Serializer(formats = ['xml','json'])
excludes = ['date_created']

class BarResource(ModelResource):
foo = fields .ForeResKey(FooResource,attribute ='foo',full = True,null = True)

class Meta:
queryset = Bar.objects.all()
resource_name =' bar'
serializer = Serializer(formats = ['xml','json'])

如果没有关系,你也可以做一些事情(机智h大数据集会导致大量数据库开销,您可能需要重新考虑模型定义):

  class FooResource(ModelResource )
class Meta:
queryset = Foo.objects.all()
resource_name ='foo'
serializer = Serializer(formats = ['xml','json'] )
excludes = ['date_created']

def脱水(self,bundle):
obj = self.obj_get(id = bundle.data ['id'])
bundle.data ['bar'] = Bar.objects.get(id = 1).name
返回包


Lets say I have three models Submission, Contact and SubmissionContact.

class Submission(models.Model):
  title = models.CharField(max_length=255, verbose_name='Title')
  ...

class Contact(models.Model):
  name = models.CharField(max_length=200, verbose_name='Contact Name')
  email = models.CharField(max_length=80, verbose_name='Contact Email')
  ...

class SubmissionContact(models.Model):
  submission = models.ForeignKey(Submission)
  contact = models.Foreign(Contact, verbose_name='Contact(s)')

Can I read / write to all these three tables using a single ModelResource using tastypie. (basically get and put actions on list and detail in tastypie)

Thanks in advance for any help.

解决方案

You can nest one model into the other or use the dehydrate cycle to add extra resources to your output, for example consider a Foo and Bar model

class FooResource(ModelResource):
    class Meta:
        queryset = Foo.objects.all()
        resource_name = 'foo'
        serializer = Serializer(formats=['xml', 'json'])
        excludes = ['date_created']

class BarResource(ModelResource):
    foo = fields.ForeignKey(FooResource, attribute='foo', full=True, null=True)

    class Meta:
        queryset = Bar.objects.all()
        resource_name = 'bar'
        serializer = Serializer(formats=['xml', 'json'])

If there's no relationship you could also do something like (with large datasets this would cause a lot of database overhead, you might have to rethink your model definitions):

class FooResource(ModelResource):
    class Meta:
        queryset = Foo.objects.all()
        resource_name = 'foo'
        serializer = Serializer(formats=['xml', 'json'])
        excludes = ['date_created']

    def dehydrate(self, bundle):
        obj = self.obj_get(id=bundle.data['id'])
        bundle.data['bar'] = Bar.objects.get(id=1).name
        return bundle

这篇关于如何结合多种风味的多种资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆