Django过滤器外键字段 [英] Django filter foreignkey field

查看:101
本文介绍了Django过滤器外键字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本:

我有一个用于食谱的Django应用,并且想要过滤要发送到模板的数据.我基本上希望将特定用户添加的所有收据作为上下文发送.以下过滤返回错误消息 int()的无效文字,其基数为10: my_username .

I have a Django app used for recipes, and want to filter data to be sent to a template in my view. I basically want all recepies that are added by a specific user to be sent as context. The following filtering returns an error message invalid literal for int() with base 10: my_username.

recipes = Recipe.objects.filter(added_by = uname)

变量 uname 是从模板传递的.另一方面,对 request.user 进行过滤可以正常工作,但不是我想要的.

The variable uname is passed from a template. On the other hand, filtering on request.user works fine, but is not what I want.

recipes = Recipe.objects.filter(added_by = request.user)

详细信息:

我的模型(相关字段)为:

My models are given (relevant fields) as:

class Recipe (models.Model):
    ...
    ...
    added_by = models.ForeignKey(User)

User 是现有的Django用户.当我在模板中调用{{recipe.added_by}}时,将获得所需的用户名.此用户名通过 href ="/profile/{{recipe.added_by}}" 传递到视图,该视图如下所示:

Where User is an existing Django user. When I call {{ recipe.added_by }} in my template, I get the username as wanted. This username is passed on to a view with href="/profile/{{recipe.added_by}}", where the view looks like the following:

def profile(request, uname):

    print uname #Correct username printed
    print request.user #Logged in user (not relevant, as userprofile should be visible for all)

    recipes = Recipe.objects.filter(added_by = uname) #Does not work. Why?
    #recipes = Recipe.objects.filter(added_by = request.user)

    form = CommentForm(request.POST)

    context = {
        'uname': uname,
        'recipes': recipes,
        'form': form,
    }
    return render(request, '*app_name*/profile.html', context)

不确定我缺少什么,但是据我所知,这似乎与 added_by 具有用户外键的事实有关.我也尝试根据[1]将过滤器参数更改为 recipe__added_by__added_by = uname ,但是Django随后返回了一个错误,提示无法将关键字'recipe'解析为字段",这似乎很明显.我的网址是:

Not sure what I am missing, but from what I can tell, it seems to have something to do with the fact that added_by has a Foreign Key to a User. I also tried to change the filter argument to recipe__added_by__added_by = uname according to [1], but Django then returned an error saying "Cannot resolve keyword 'recipe' into field", which seems obvious. My url is:

url(r'^profile/([a-zA-Z0-9]+)/$', 'profile', name='*app_name*-profile'),

感谢您的答复.抱歉,这应该很明显.

Thanks for any reply. Sorry if this should have been obvious.

[1] 按外键对Django模型进行过滤

推荐答案

您可以尝试:

 recipes = Recipe.objects.filter(added_by__username = uname)

并且 request.user 对于 Recipe.objects.filter(added_by = request.user)正常工作,因为 request.user 是一个对象.详细信息: https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

And request.user works fine for Recipe.objects.filter(added_by = request.user) because request.user is an object. details: https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

这篇关于Django过滤器外键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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