我试图为我的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)
但问题是,稍后在我的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 %}
提前致谢
编辑:这是我使用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
和我的views.py:
class VotesView(generic.ListView):
template_name = 'polls/votes.html'
model = Question
context_object_name = 'latest_question_list'
答案 0 :(得分:0)
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)
修改强>
我再次将only
更改为values_list
,我知道你正在填充所有用户ID和用户名,并将它们存储为元组以制作选项列表。
现在在你的模板中你应该这样做
{% 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 %}