gpt4 book ai didi

用于外键的 Django Tastypie apply_filter

转载 作者:行者123 更新时间:2023-12-02 01:47:14 24 4
gpt4 key购买 nike

我有两个带有外键 -user 的关系表。而返回给我的queryset如下

city: null,
contact_address: "",
contact_number: "9874563210",
country: null,
date_of_birth: null,
id: 12,
invitation_text: "Aphabets",
middle_name: "def",
profile_img: null,
resource_uri: "/client/api/v1/profile/12/",
state: null,
user: {
date_joined: "2014-07-27T04:07:00",
email: "abc@cba.com",
first_name: "abc",
id: 24,
is_active: false,
last_login: "2014-07-27T04:07:00",
last_name: "ghi",
resource_uri: "/client/api/v1/user/24/",
username: "abc@cba.com"
},
zipcode: n

我的api资源如下:

类配置文件资源(模型资源):

user = fields.ToOneField(UserResource,'user', full=True)    

"""docstring for ProfileResource"""
def alter_list_data_to_serialize(self, request, data):
if request.GET.get('meta_only'):
return {'meta': data['meta']}
return data

def authorized_read_list(self, object_list, bundle):

if (bundle.request.user.is_superuser is True) or (bundle.request.is_staff is True) :
return object_list.all().select_related()
else:
return object_list.filter(user=bundle.request.user).select_related()

def obj_create(self, bundle, **kwargs):
return super(ProfileResource, self).obj_create(bundle, user=bundle.request.user)

class Meta:
queryset = UserProfile.objects.filter()
resource_name = 'profile'
default_format = 'application/json'
list_allowed_methods = ['get','post']
detail_allowed_methods = ['get', 'post', 'put', 'delete', 'patch']
serializer = Serializer()
default_format = 'application/json'
authentication = Authentication()
authorization = DjangoAuthorization()
always_return_data = True

我需要过滤查询集,以便像过滤器一样只返回活跃用户。

当我尝试 object_list.filter(is_active = true) 时。我收到一条错误消息,指出“is_active”不是有效选项,选项为“城市”、“国家/地区”等。如何在过滤器中引用关系表值?

我看到了覆盖 apply_filter 方法的建议..但我的问题是..有没有办法使用外键值进行过滤?

请指导我完成这一部分。提前致谢。

最佳答案

尝试:

object_list.filter(user__is_active = true)

您可以在此处找到相关文档:https://docs.djangoproject.com/en/1.6/topics/db/queries/#lookups-that-span-relationships

关于用于外键的 Django Tastypie apply_filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24991530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com