由查询集填充的 Django 选择字段 - 保存 id 但显示值
Django choice field populated by queryset - save id but show value
我有两个模型,Tag
和 TagGroup
。
class TagGroup(models.Model):
tag_group_name = models.CharField(max_length=100)
class Tag(models.Model):
tag_name = models.CharField(max_length=100)
tag_group = models.ForeignKey(TagGroup, blank=True, null=True)
我已将 TagGroup
表单作为选择字段放入模板中,以便我可以将 TagGroup
分配给标签。我创建此表单是为了从 TagGroup
查询集进行填充。
class TagGroupForm(ModelForm):
tag_group_name = forms.ModelChoiceField(queryset=TagGroup.objects.values_list('id', 'tag_group_name'), required=False)
class Meta:
model = TagGroup
fields = [
'tag_group_name'
]
我没有看到任何明显的说明如何将 ID 分配给标签 table,同时在模板的选择字段中仅向用户显示标签值。
目前以上显示:
几个问题:
查询集是否正确?我尝试过不使用“values_list
”,但它只是在模板的表单字段中显示 "Object"?
我如何 'hide' Id 以便我可以保存它,同时仅向用户显示表单字段中的实际字符串值?
编辑以添加更新的表格:
class TagGroupForm(ModelForm):
tag_group_name = forms.ModelChoiceField(queryset=TagGroup.objects.all(), to_field_name = 'tag_group_name', required=False)
class Meta:
model = TagGroup
fields = [
'tag_group_name'
]
现在生成以下内容.. 看起来很接近.. 表单值有一个很好的字符串,但显示给用户的实际值仍然是 "TagGroup object"。如何让它显示?
来自 the docs、
The str (unicode on Python 2) method of the model will be called to generate string representations of the objects for use
所以只需将其分配给对象名称就可以了! (此外,您不需要使用 values_list
)它默认显示对象的原因是因为这是默认的字符串表示形式。
class TagGroup(models.Model):
tag_group_name = models.CharField(max_length=100)
def __str__(self):
return self.tag_group_name
tag_group_name = forms.ModelChoiceField(queryset=TagGroup.objects.all(), required=False)
或者,如果您不想修改它并希望将其保留作其他用途。
To provide customized representations, subclass ModelChoiceField and override label_from_instance
. This method will receive a model object, and should return a string suitable for representing it
class TagChoiceField(ModelChoiceField):
queryset = TagGroup.objects.all()
def label_from_instance(self, obj):
return obj.tag_group_name # or similar
我有两个模型,Tag
和 TagGroup
。
class TagGroup(models.Model):
tag_group_name = models.CharField(max_length=100)
class Tag(models.Model):
tag_name = models.CharField(max_length=100)
tag_group = models.ForeignKey(TagGroup, blank=True, null=True)
我已将 TagGroup
表单作为选择字段放入模板中,以便我可以将 TagGroup
分配给标签。我创建此表单是为了从 TagGroup
查询集进行填充。
class TagGroupForm(ModelForm):
tag_group_name = forms.ModelChoiceField(queryset=TagGroup.objects.values_list('id', 'tag_group_name'), required=False)
class Meta:
model = TagGroup
fields = [
'tag_group_name'
]
我没有看到任何明显的说明如何将 ID 分配给标签 table,同时在模板的选择字段中仅向用户显示标签值。
目前以上显示:
几个问题:
查询集是否正确?我尝试过不使用“
values_list
”,但它只是在模板的表单字段中显示 "Object"?我如何 'hide' Id 以便我可以保存它,同时仅向用户显示表单字段中的实际字符串值?
编辑以添加更新的表格:
class TagGroupForm(ModelForm):
tag_group_name = forms.ModelChoiceField(queryset=TagGroup.objects.all(), to_field_name = 'tag_group_name', required=False)
class Meta:
model = TagGroup
fields = [
'tag_group_name'
]
现在生成以下内容.. 看起来很接近.. 表单值有一个很好的字符串,但显示给用户的实际值仍然是 "TagGroup object"。如何让它显示?
来自 the docs、
The str (unicode on Python 2) method of the model will be called to generate string representations of the objects for use
所以只需将其分配给对象名称就可以了! (此外,您不需要使用 values_list
)它默认显示对象的原因是因为这是默认的字符串表示形式。
class TagGroup(models.Model):
tag_group_name = models.CharField(max_length=100)
def __str__(self):
return self.tag_group_name
tag_group_name = forms.ModelChoiceField(queryset=TagGroup.objects.all(), required=False)
或者,如果您不想修改它并希望将其保留作其他用途。
To provide customized representations, subclass ModelChoiceField and override
label_from_instance
. This method will receive a model object, and should return a string suitable for representing it
class TagChoiceField(ModelChoiceField):
queryset = TagGroup.objects.all()
def label_from_instance(self, obj):
return obj.tag_group_name # or similar