我想使用从我的模型名称列中输出值forms.ModelChoiceField
。我已经阅读了很多关于该主题的文档,但事实证明很难达到预期的结果!
这是我的代码。
我有这个表格:
表格.py
from django import forms
from asset_db.models import Profiles
class SubmitJob(forms.Form):
material_id = forms.CharField(max_length=8)
workflow = forms.ModelChoiceField(queryset=Profiles.objects.distinct('name'))
start_datepicker = forms.CharField(max_length=12)
end_datepicker = forms.CharField(max_length=12)
这个模型:
class Profiles(models.Model):
id = models.DecimalField(6).auto_creation_counter
name = models.CharField(max_length=256, default='null')
target_path = models.CharField(max_length=256, default='null')
target_profile = models.TextField(max_length=1024, default='null')
package_type = models.CharField(max_length=256, default='null')
video_naming_convention = models.CharField(max_length=256, default='null')
image_naming_convention = models.CharField(max_length=256, default='null')
package_naming_convention = models.CharField(max_length=256, default='null')
def __str__(self):
return self.name
这是 HTML 输出:
<p><label for="id_workflow">Workflow:</label> <select id="id_workflow" name="workflow" required>
<option value="" selected="selected">---------</option>
<option value="2">Amazon</option>
<option value="1">Test</option>
</select></p>
我想知道如何在我的模型中将“value”的值更改为“name”列,例如:
<p><label for="id_workflow">Workflow:</label> <select id="id_workflow" name="workflow" required>
<option value="" selected="selected">---------</option>
<option value="Amazon">Amazon</option>
<option value="Test">Test</option>
</select></p>
提前致谢。