Django-模型字段的自定义属性
Django中有没有一种方法可以将自定义属性添加到模型的字段中(而无需对字段进行子类化) 我只想在模板的某些部分显示某些字段。这是因为最终每种类型的字段都将显示在单独的选项卡中。我考虑在每个字段中添加一个自定义属性,以确定它应该放在哪个节/选项卡中。但是,到目前为止,我运气不好 我有几种字段类型:Django-模型字段的自定义属性,django,tabs,Django,Tabs,Django中有没有一种方法可以将自定义属性添加到模型的字段中(而无需对字段进行子类化) 我只想在模板的某些部分显示某些字段。这是因为最终每种类型的字段都将显示在单独的选项卡中。我考虑在每个字段中添加一个自定义属性,以确定它应该放在哪个节/选项卡中。但是,到目前为止,我运气不好 我有几种字段类型: class Enum(set): def __getattr__(self, name): if name in self: return name
class Enum(set):
def __getattr__(self, name):
if name in self:
return name
raise AttributeError
FieldTypes = Enum(["one","two","three",])
class FieldType(object):
def __init__(self, type=None, name=None):
self._type = type
self._name = name
def getType(self):
return self._type
def getName(self):
return self._name
class FieldTypeList(deque):
def __getattr__(self,type):
for ft in self:
if ft.getType() == type:
return ft
raise AttributeError
FieldTypes = FieldTypeList([FieldType("ONE","The Name Of Field One"),FieldType("TWO","The Name Of Field Two")])
还有一些型号:
class Model1(models.Model):
a = models.CharField()
b = models.ForeignKey('Model2')
c = models.IntegerField()
a.type = FieldTypes.one # this obviously doesn't work
b.type = FieldTypes.two # this obviously doesn't work
c.type = FieldTypes.three # this obviously doesn't work
class Model2(models.Model):
d = models.CharField()
和一份表格:
class Form1(forms.ModelForm):
class Meta:
model = Mode1
class Form1(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(Form1, self).__init__(*args, **kwargs)
self.fields['a'].type = FieldTypes.one
self.fields['b'].type = FieldTypes.two
self.fields['c'].type = FieldTypes.three
class Meta:
model = Mode1
和一个模板:
{% for fieldType in FieldTypes %}
<div class="{{fieldType}}">
{% for field in form %}
{% if field.type = fieldType %}
{{ field }}
{% endif %}
{% endfor %}
</div>
{% endfor %}
{%forfieldtypes%}
{%形式的字段为%}
{%if field.type=fieldType%}
{{field}}
{%endif%}
{%endfor%}
{%endfor%}
但这不起作用
想法?或者其他只在页面的某些部分放置某些字段的建议
谢谢。一般来说,我会将此逻辑保留在模型类之外。如果您可以帮助的话,模型不应该与表示元素纠缠在一起,选择在表单中显示哪些字段似乎是一个表示问题。幸运的是,Form类在数据层(模型)和表示层(视图和模板)之间提供了一个漂亮的、以UI为中心的层 这是我过去如何解决这个问题的。在我的Form类中,我创建了一个字段组列表,每个字段组都有一个标题和它们所包含字段的名称列表:
class MyModelForm(forms.ModelForm):
field_groups = (
{'name':'Group One', 'fields':('a', 'b', 'c')},
{'name':'Group Two', 'fields':('d', 'e')},
)
class Meta:
model = MyModel
然后在模板中,我在组中循环,并在循环中有条件地包含这些字段:
{% for group in form.field_groups %}
<h3 class="groupheader">{{group.name}}</h3>
{% for field in form %}
{% if field.name in group.fields %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endif %}
{% endfor %}
{% endfor %}
{%用于form.field_groups%}
{{group.name}
{%形式的字段为%}
{%if group.fields%中的field.name}
{{field.errors}}
{{field.label_tag}}:{{{field}
{%endif%}
{%endfor%}
{%endfor%}
这允许您控制MyModelForm
类中表单字段的分组和显示,这是表示逻辑的合理位置。这是可能的
class Model1(models.Model):
a = models.CharField()
b = models.ForeignKey('Model2')
c = models.IntegerField()
和一份表格:
class Form1(forms.ModelForm):
class Meta:
model = Mode1
class Form1(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(Form1, self).__init__(*args, **kwargs)
self.fields['a'].type = FieldTypes.one
self.fields['b'].type = FieldTypes.two
self.fields['c'].type = FieldTypes.three
class Meta:
model = Mode1
事实证明,我确实希望在我的模型类中使用这种逻辑;字段类型不仅仅用于确定在何处/如何显示它们(尽管它们也用于此)。我提出了以下解决方案 我定义了一些类来存储一组字段类型:
class Enum(set):
def __getattr__(self, name):
if name in self:
return name
raise AttributeError
FieldTypes = Enum(["one","two","three",])
class FieldType(object):
def __init__(self, type=None, name=None):
self._type = type
self._name = name
def getType(self):
return self._type
def getName(self):
return self._name
class FieldTypeList(deque):
def __getattr__(self,type):
for ft in self:
if ft.getType() == type:
return ft
raise AttributeError
FieldTypes = FieldTypeList([FieldType("ONE","The Name Of Field One"),FieldType("TWO","The Name Of Field Two")])
还有一些模型,每个模型都有一组从字段类型到特定字段名的映射(在本例中,字段“a”、“b”和“c”的类型为“一”,字段“d”的类型为“二”):
我有一个基本表格:
class MyForm(forms.ModelForm):
class Meta:
model = ChildModel
以及一个自定义过滤器,用于从特定表单返回给定类型的所有字段(注意,通过其元类从表单访问模型):
最后,还有一个模板,用于将所有内容组合在一起(该模板采用MyForm和FieldType):
{%for type in type%}
{%with fieldsOfType=form | getFieldsOfType:type%}
{%形式的字段为%}
{%if FieldSoftType%中的field.name}
{{field.errors}}
{{field.label_tag}}:{{{field}
{%endif%}
{%endfor%}
{%endwith%}
{%endfor%}
这适用于Django 3.1.5。在模板中的字段上循环时,可以通过“field.field.type”访问字段的属性(例如“type”)。