Tastypie + Django:如何过滤子文档?

时间:2014-03-18 18:08:08

标签: django django-models tastypie django-mongodb-engine

我正在尝试在我的一个基于Django(非rel,mongodb-engine)+ Tastypie(非rel)的数据API中实现过滤。我想在主文档中的子文档上实现过滤。 mongo中的主要文档看起来像

{"user_name": "abc", "email": "abc@domain.com", "safety" :{"intra": True, "inter": False, "external": ['a', 'b']}}

子文档包含2个布尔字段和列表字段。在mongo中我可以轻松查询子文档及其字段但是我无法在API级别实现它.django模型看起来像这样

from djangotoolbox.fields import DictField, EmbeddedModelField
class UserSafety(models.Model):
   user_name = models.CharField()
   email = models.EmailField()
   safety = DictField()   <------- What to use here!

相应的Tasty-pie资源看起来像

 from tastypie_nonrel.fields import DictField, EmbeddedModelField
 class UserSafetyResource(ModelResource):
    user_name = field.CharField(attribute='user_name')
    email = fields.CharField(attribute='email')
    safety = DictField(attribute='safety')   <----What to use here!

    # This part i want to add
    class Meta:
       filtering = ['safety.intra': ALL, 'safety.inter': ALL]

最终我希望它能像在HTTP请求中那样工作

GET http://<server>/my_endpoint/usersafety?safety.intra=true

有关如何实现这一目标的任何想法?可能正在使用嵌入式字段?

0 个答案:

没有答案