[英]Creating list of usernames from User model in Django
Im trying to make a user list for my multiselectfield. 我正在尝试为我的multiselectfield创建用户列表。
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
users = User.objects.values_list('id','username')
authorized = MultiSelectField(choices=users, null=True)
but the problem is that later on in my html file i can't compare those values with a logged user id 但是问题是,稍后在我的html文件中,我无法将这些值与登录的用户ID进行比较
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
{% for person in question.authorized %}
{% ifequal person request.user.id %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endifequal %}
{% endfor %}
</ul>
{% endfor %}
Thanks in advance 提前致谢
EDIT: here's my traceback calls after use only() instead of values_list(): 编辑:这是我在使用only()而不是values_list()之后的回溯调用:
Traceback (most recent call last):
File "/home/ab/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/ab/.local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
autoreload.raise_last_exception()
File "/home/ab/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "/home/ab/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 327, in execute
autoreload.check_errors(django.setup)()
File "/home/ab/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/ab/.local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/ab/.local/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/home/ab/.local/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/ab/Documents/szkieletowe/projectmaster/DjangoPollsApp/PollsApp/polls/models.py", line 8, in <module>
class Question(models.Model):
File "/home/ab/Documents/szkieletowe/projectmaster/DjangoPollsApp/PollsApp/polls/models.py", line 12, in Question
authorized = MultiSelectField(choices=users, null=True)
File "/home/ab/.local/lib/python3.6/site-packages/multiselectfield/db/fields.py", line 69, in __init__
self.max_length = get_max_length(self.choices, self.max_length)
File "/home/ab/.local/lib/python3.6/site-packages/multiselectfield/utils.py", line 31, in get_max_length
return len(','.join([string_type(key) for key, label in choices]))
File "/home/ab/.local/lib/python3.6/site-packages/multiselectfield/utils.py", line 31, in <listcomp>
return len(','.join([string_type(key) for key, label in choices]))
TypeError: 'User' object is not iterable
and my views.py: 和我的views.py:
class VotesView(generic.ListView):
template_name = 'polls/votes.html'
model = Question
context_object_name = 'latest_question_list'
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
users = User.objects.values_list('id','username')
authorized = MultiSelectField(choices=users, null=True)
edit 编辑
I have changed only
to values_list
again, i got it you are populating all the users id and username and storing them as tuple to make a choice list. 我
only
再次更改为values_list
,我明白了您正在填充所有用户ID和用户名并将其存储为元组以做出选择列表。
now in your template you should do like this 现在在您的模板中,您应该这样做
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
{% for person in question.authorized %}
{% ifequal person request.user.username %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endifequal %}
{% endfor %}
</ul>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.