我有模型:课程和带有中间连接模型 coursetag 的标签(用于多对多关系)。
在我的 API 中,TagResource 显示标签以及课程数量。然后,当用户(通过 API)请求特定标签时,他们会获得带有指定标签的所有课程的列表。所有这一切都很好......但是,我现在需要能够根据用户过滤课程(无论他们是否将 user.is_staff 设置为 True )。
我可以让它在标签列表 API 中显示正确的课程数量,但是我在弄清楚如何在选择特定标签时过滤课程时遇到问题。
我的 API 资源如下:
class TagResource(ModelResource):
count = fields.IntegerField(readonly=True)
courses = fields.ToManyField('oppia.api.resources.CourseTagResource', 'coursetag_set', related_name='tag', full=True)
class Meta:
queryset = Tag.objects.filter(courses__isnull=False).distinct().order_by("name")
resource_name = 'tag'
allowed_methods = ['get']
fields = ['id','name']
authentication = ApiKeyAuthentication()
authorization = ReadOnlyAuthorization()
always_return_data = True
include_resource_uri = False
serializer = TagJSONSerializer()
def dehydrate_count(self,bundle):
if bundle.request.user.is_staff:
count = Course.objects.filter(tag__id=bundle.obj.id).count()
else:
count = Course.objects.filter(tag__id=bundle.obj.id, staff_only=False).count()
return count
class CourseResource(ModelResource):
class Meta:
queryset = Course.objects.all()
resource_name = 'course'
allowed_methods = ['get']
fields = ['id', 'title', 'version', 'shortname']
authentication = ApiKeyAuthentication()
authorization = ReadOnlyAuthorization()
serializer = CourseJSONSerializer()
always_return_data = True
include_resource_uri = True
class CourseTagResource(ModelResource):
course = fields.ToOneField('oppia.api.resources.CourseResource', 'course', full=True)
class Meta:
queryset = CourseTag.objects.all()
allowed_methods = ['get']
fields = ['id','course','tag']
include_resource_uri = False
authentication = ApiKeyAuthentication()
authorization = ReadOnlyAuthorization()
always_return_data = True
示例请求/响应如下:
对于标签列表(例如http://localhost/python/api/v1/tag/?username=XXXetc
),我得到了预期的响应:
{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 10}, "tags": [{"count": 1, "id": 8, "name": "34343434"}, {"count": 2, "id": 2, "name": "Education"}, {"count": 1, "id": 7, "name": "Engineering"}, {"count": 1, "id": 13, "name": "Ethiopia"}, {"count": 1, "id": 10, "name": "health"}, {"count": 5, "id": 1, "name": "Healthcare"}, {"count": 1, "id": 11, "name": "phone"}, {"count": 1, "id": 12, "name": "PNC"}, {"count": 1, "id": 6, "name": "Solar"}, {"count": 1, "id": 9, "name": "test"}]}
但是对于特定标签(例如http://localhost/python/api/v1/tag/13/?username=XXXetc
),我得到正确的计数(1 门课程),但错误的课程列表(2 门课程):
{"count": 1, "courses": [{"id": 2, "resource_uri": "/python/api/v1/course/2/", "shortname": "pnc", "title": {"en": "Postnatal Care"}, "url": "http://localhost/python/api/v1/course/2/download/", "version": "20131105085218"}, {"id": 30, "resource_uri": "/python/api/v1/course/30/", "shortname": "anc2", "title": {"en": "Antenatal Care v2"}, "url": "http://localhost/python/api/v1/course/30/download/", "version": "20131106162547"}], "id": 13, "name": "Ethiopia"}
我尝试使用与此处给出的方法类似的方法:How to filter ToManyField of django-tastypie by request.user? 但我不知道如何根据 user.is_staff 来应用 lambda 函数。
我还尝试对 CourseResource 和 CourseTagResource 设置授权限制,但这似乎也没有任何区别。
有关信息,我使用的是 sweetpie 0.9.16 和 Django 1.5。
任何帮助/指针都非常感谢,任何关于我是否采用正确方法的建议都会有所帮助 - 我不确定我是否应该查看课程模型权限?
谢谢