【问题标题】:Get all the Booleans of Model accordingly相应地获取模型的所有布尔值
【发布时间】:2021-12-19 10:50:51
【问题描述】:

我正在构建一个博客应用程序,我正在尝试获取Profile 模型的所有布尔值。我之前尝试过制作一个布尔值列表,但它不符合要求。

文件models.py

class Profile (models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, unique=True)
    name = models.CharField(max_length=30)
    first_boolean = models.BooleanField(default=False)
    second_boolean = models.BooleanField(default=False)
    third_boolean = models.BooleanField(default=False)

文件views.py

def page(request):
    All_Booleans = Profile.objects.filter()

    context = {'Booleans_List'}
    return render(request, 'page.html', context)

我还尝试了from django.db.models import F 中的 F 之类的:

All_Booleans = Profile.objects.filter(F(first_boolean=request.user) | F(second_boolean=request.user))

但它正在显示:

init() 得到了一个意外的关键字参数“first_boolean”

我想做什么? -

我正在尝试获取 request.user 的 Profile Model 的所有布尔字段

但是我该怎么做呢?

【问题讨论】:

  • 我仍然不清楚你的目标是什么。如果你排除了为真的布尔值,你会得到一个由零个、一个、两个或三个布尔值组成的列表,所有这些布尔值都是False。但是我不清楚你想用那个列表做什么。此外,request.user.profile 将不起作用,因为用户可以与您的建模中的许多 Profiles 相关。
  • 我会跟踪每个Boolean,如果它不起作用,我怎样才能得到所有Booleans of a Model
  • @Van 请参阅What is the XY problem? 更不用说我们不知道这里的 X 是什么,我连这里的 Y 都无法理解。请在您的问题中添加更多详细信息,您到底想做什么?
  • @Van:我根本不明白你想做什么。取决于它可能会起作用。但是您可以简单地将profile 传递给模板,并在那里渲染布尔值。但我认为现在要解决的第一个问题是检索User.profile。由于您使用的是ForeignKey,而不是OneToOneField,这意味着用户对象中没有添加.profile

标签: python django django-models django-views django-queryset


【解决方案1】:

您可以通过以下方式获取登录用户的Profile

from django.contrib.auth.decorators import login_required

@login_required
def page(request):
    profile = Profile.objects.get(user=request.user)
    return render(request, 'page.html', {'profile': profile})

然后,您可以在模板中呈现 profile 的布尔字段:

one: {{ profile.first_boolean }}
two: {{ profile.second_boolean }}
three: {{ profile.third_boolean }}

您可以将布尔值添加到列表中并在模板中使用它们:

@login_required
def page(request):
    profile = Profile.objects.get(user=request.user)
    data = [profile.first_boolean, profile.second_boolean, profile.third_boolean]
    return render(request, 'page.html', {'profile': profile, 'data': data})

然后,例如,您可以使用以下方式渲染它:

{% for item in data %}
    {{ item }}
{% endfor %}

注意:您可以使用 @login_required decorator [Django-doc].


注意:通常使用settings.AUTH_USER_MODEL [Django-doc]来引用用户模型比直接使用User model [Django-doc]更好。

更多信息,您可以查看referencing the User model section of the documentation

【讨论】:

  • 谢谢,但我认为你不明白一些事情,我正在尝试获取所有布尔字段,例如 queryset,例如:- [<first_boolean:True>,<second_boolean:False>] 只是布尔字段。但我喜欢你的settings.AUTH_USER_MODEL 建议
  • @Van:您可以将这些放入列表中(请参阅编辑后的答案),例如枚举这些,但随后该字段的 name 的数据消失了.您还可以构造一个 2 元组列表,例如 [('first_boolean, profile.first_boolean)] 等,但我仍然不清楚您为什么要这样做:通常如果您制作不同的列,将它们作为单独的列没有多大意义列表中的元素。如果这是必要的,那么这通常意味着建模有问题。
  • 我会一一跟踪每一个Boolean Field,并监控转布尔字段True的需求。如何在此循环中访问列表的第一个元素,我在模板中尝试了 .0.first 但它没有显示任何内容
  • @Van:在循环中,item 每次都是列表的一个元素。您可以使用{{ data.0 }} 访问列表的第一项,但这相当于{{ profile.first_boolean }},因此不清楚为什么需要列表。
猜你喜欢
  • 1970-01-01
  • 2012-08-21
  • 2011-01-15
  • 2013-12-22
  • 2016-08-13
  • 2018-05-31
  • 1970-01-01
  • 2015-04-01
  • 2022-01-21
相关资源
最近更新 更多