Hello,

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

I would like to only display certain fields in certain sections of my template. (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 it should go in. But, so far, I've had no luck.

I have a few field types:

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

FieldTypes = Enum(["one","two","three",])
</snip>

And a few models:

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

And a form:

<snip>
class Form1(forms.ModelForm):
  class Meta:
    model = Mode1
</snip>

And a template:

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

But this doesn't work.

Ideas? Or another suggestion for only placing certain fields in certain sections of the page?

Thanks.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to