Django - 模型字段的自定义属性 [英] Django - custom attributes for model fields

查看:49
本文介绍了Django - 模型字段的自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 中是否有一种方法可以将自定义属性添加到模型的字段中(无需使用子类化字段)?

我只想在模板的某些部分显示某些字段.这是因为最终每种类型的字段都将显示在单独的选项卡中.我想为每个字段添加一个自定义属性,以确定它应该进入哪个部分/选项卡.但是,到目前为止,我没有运气.

我有几个字段类型:

class Enum(set):def __getattr__(self, name):如果自己的名字:返回名称引发 AttributeErrorFieldTypes = Enum(["一","二","三",])

还有一些模型:

class Model1(models.Model):a = 模型.CharField()b = models.ForeignKey('Model2')c = models.IntegerField()a.type = FieldTypes.one # 这显然不起作用b.type = FieldTypes.two # 这显然不起作用c.type = FieldTypes.three # 这显然不起作用类 Model2(models.Model):d = 模型.CharField()

还有一个表格:

class Form1(forms.ModelForm):元类:模型 = 模式 1

还有一个模板:

{% for fieldType in FieldTypes %}<div class="{{fieldType}}">{% 用于表单 %} 中的字段{% if field.type = fieldType %}{{ 场地 }}{% 万一 %}{% 结束为 %}

{% 结束为 %}

但这不起作用.

想法?或仅在页面的某些部分放置某些字段的其他建议?

谢谢.

解决方案

一般来说,我会将这个逻辑保留在模型类之外.如果您可以提供帮助,模型不应该与演示元素纠缠在一起,并且选择要在表单中显示的字段似乎是一个演示问题.幸运的是,Form 类在数据层(模型)和表示层(视图和模板)之间提供了一个很好的、专注于 UI 的层.

以下是我过去解决此问题的方法.在我的 Form 类中,我创建了一个字段组列表,每个字段组都有一个标题和一个包含字段名称的列表:

class MyModelForm(forms.ModelForm):field_groups = ({'name':'Group One', 'fields':('a', 'b', 'c')},{'name':'第二组', 'fields':('d', 'e')},)元类:模型 = 我的模型

然后在模板中,我遍历组,并在该循环中有条件地包含这些字段:

{% for group in form.field_groups %}<h3 class="groupheader">{{group.name}}</h3>{% 用于表单 %} 中的字段{% if field.name in group.fields %}<div class="fieldWrapper">{{ field.errors }}{{ field.label_tag }}: {{ field }}

{% 万一 %}{% 结束为 %}{% 结束为 %}

这允许您在 MyModelForm 类中控制表单字段的分组和显示,这是表示逻辑存在的合理位置.

Is there a way in Django to add custom attributes to a model's fields (without resorting to subclassing fields)?

I would like to only display certain fields in certain sections of my template. This is because eventually each type of field will be displayed in a separate tab. I thought about adding a custom attribute to each field to identify which section/tab it should go in. But, so far, I've had no luck.

I have a few field types:

class Enum(set):
    def __getattr__(self, name):
        if name in self:
            return name
        raise AttributeError

FieldTypes = Enum(["one","two","three",])

And a few models:

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()

And a form:

class Form1(forms.ModelForm):
  class Meta:
    model = Mode1

And a template:

{% for fieldType in FieldTypes %}
  <div class="{{fieldType}}">
      {% for field in form %}
        {% if field.type = fieldType %}
          {{ field }}
         {% endif %}
      {% endfor %} 
  </div>
{% endfor %}

But this doesn't work.

Ideas? Or other suggestions for only placing certain fields in certain sections of the page?

Thanks.

解决方案

In general, I would keep this logic outside of the model class. Models shouldn't be tangled up with presentation elements if you can help it, and choosing which fields to display in a form seems like a presentation concern. Fortunately, the Form class provides a nice, UI-focused layer between the data layer (the model) and the presentation layer (the view and template).

Here's how I've addressed this in the past. In my Form class, I created a list of field groups, each with a title and a list of the names of the fields they contain:

class MyModelForm(forms.ModelForm):
    field_groups = (
        {'name':'Group One', 'fields':('a', 'b', 'c')},
        {'name':'Group Two', 'fields':('d', 'e')},
    )
    class Meta:
        model = MyModel

Then in the template, I looped through the groups, and within that loop conditionally included those fields:

{% 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 %}

This allows you to control the grouping and display of form fields within the MyModelForm class, which is a reasonable place for presentation logic to live.

这篇关于Django - 模型字段的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆