- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
模型.py
class User(models.Model):
id = models.CharField(max_length=255)
name = models.CharField(max_length=255)
desc = models.TextField()
created_at = models.DateTimeField(auto_now=True)
updated_at = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return self.user.name
我的表单.py
class PostsForm(forms.ModelForm):
user = forms.ModelMultipleChoiceField(
queryset=User.objects.all()
)
class Meta:
model = Posts
fields = '__all__'
在我上面的代码中,我的表单如下所示:
+------------+
|Select + |
+------------+
|John Doe |
|Jessica |
|Jessica |
|Alex Joe |
+------------+
不过这不是我想要的,因为可能有多个名称,而且我知道该值来 self 模型上的 def __str__
。
如何在我的字段上添加另一个 str so 将是:
+---------------------+
|Select + |
+---------------------+
| 012931 - John Doe |
| 012932 - Jessica |
| 012933 - Jessica |
| 012934 - Alex Joe |
+---------------------+
它包含下拉列表中的 id + name,或其他信息。
最佳答案
在 __str__
上的 User
模型中,您可以按照 doc 中的说明进行操作.
def __str__(self):
return "{} - {}".format(self.id, self.user.name)
这里是self.id
的一个例子,你可以在那里得到任何值,比如self.name
等等。
关于python - ModelMultipleChoiceField 上的 Django 多值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50771539/
class AddRoleForm(forms.Form): roles=forms.ModelMultipleChoiceField(queryset=Role.objects.all()
我的模型表单有一个带有 ModelMultipleChoiceField 的字段,该字段具有到 Category 对象的查询集。问题是表单提交,但类别数据没有保存。想法? 非常感谢! 表格: clas
在 Django 中,如何将 ModelMultipleChoiceField 渲染为多列 ? fav = forms.ModelMultipleChoiceField(Show.objects.a
在过去的几个小时里,我一直在搜索并试图让它工作,但我做不到 我有一个从数据库中获取的技能列表: forms.ModelMultipleChoiceField(
我正在尝试做一些我认为很常见的事情,但我真的不确定如何解决这个问题。 我目前在做什么:目前,我在表单上生成了一个复选框列表(使用“CheckboxSelectMultiple”),它会自动勾选 dja
ModelMultipleChoiceField 在模板中显示是一个复选框列表,带有对应对象的 unicode 表示。如何以表格形式显示 ModelMultipleChoiceField,任意列中的任
我有一个与类别有很多关系的配置文件对象 class Profile(models.Model): . . . category = models.ManyToManyField(Cat
{% for choice in form.options.field.choices %} {{ choice.1 }}
我正在尝试从我的 postgres 数据库的 View 中创建多选字段,但是网页显示不正确。具体来说,虽然它提供了正确数量的选择,但它没有通过“check_name”字段命名它们,而是将每个选择命名为
我不希望登录用户出现在这个 ModelMultipleChoiceField 上,以限制他们自己与自己建立关注关系?那么我如何从查询集中排除已登录的用户,这可能是一个简单的解决方法,但我是 Djang
我有如下形式和模型。我正在使用 Django 1.6.4。我能够使用基于参数的默认检查选项创建多项选择字段: self.fields["options"].initial 但是如何将这个选中的选项设置
我正在尝试使用内置的 django 管理小部件 ModelMultipleChoiceField 以表单形式呈现类似这样的内容: 我遵循了其他人的建议here ,并查看了documentation 。
我在两个不同的地方使用一段代码来动态生成一些表单字段。在这两种情况下,dynamic_fields 都是一个字典,其中键是对象,值是对象列表(如果列表为空,则值为 False): class Exam
我已经看过所有有关如何设置 ModelMultipleChoiceField 以使用自定义查询集的方法,并且我已经尝试过它们并且它们有效。然而,它们都使用相同的范例:查询集只是相同对象的过滤列表。 就
假设我有一些人为设计的模型: class Author(Model): name = CharField() class Book(Model): title = CharField()
ModelMultipleChoiceField 不选择初始选择,我无法在我的示例中进行以下修复(下面的链接): http://code.djangoproject.com/ticket/5247#c
我有以下表格: from models import MyModel class MultProgresemosReferenciasForm(forms.Form): selected =
我在获取要为 Django 表单中的特定字段显示的验证错误消息时遇到问题,其中有问题的字段是 ModelMultipleChoiceField。 在表单的 clean(self) 方法中,我尝试将错误
我在使用 ModelMultipleChoiceField 设置表单时遇到问题,其中查询集取决于用户。我的目标是实现导出功能。 我的观点如下: class ExportView(FormView):
我正在编写一个酒店预订系统。我有一个名为“预订”的模型。我可以创建包含开始日期、结束日期的预订、预订一间房间并分配 1 位客人。然而,当我去修改模型、表单和 View 以保存超过 1 个房间和客人时,
我是一名优秀的程序员,十分优秀!