Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6904703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:05:39+00:00 2026-05-27T08:05:39+00:00

Is there a way in Django to add custom attributes to a model’s fields

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:05:40+00:00Added an answer on May 27, 2026 at 8:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a way l can compare two columns | fields in django like
Is there a way to add custom views to the Django admin app? To
Is there a way to specify a Model in Django such that is ensures
Is there a recommended way of using Django to clean an input string without
is there a way to do the following in one Django query? MyModel.filter(attr=a).values('attr','fields','of','interest').annotate(count=Count('id')) MyModel.filter(attr=b).values('attr','fields','of','interest').annotate(count=Count('id'))
Is there a way to add this chat in my django project? : http://www.cometchat.com/
Is there a way to remove the Add functionality on the Django admin site?
I have a django model called Blog . I'd like to add a field
I am using Djangos default authentication system (django.contrib.auth) and I would like to add
I need to add a FULLTEXT index to one of my Django model's fields

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.