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 81751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:30:32+00:00 2026-05-10T21:30:32+00:00

In a Django form, how do I make a field read-only (or disabled)? When

  • 0

In a Django form, how do I make a field read-only (or disabled)?

When the form is being used to create a new entry, all fields should be enabled – but when the record is in update mode some fields need to be read-only.

For example, when creating a new Item model, all fields must be editable, but while updating the record, is there a way to disable the sku field so that it is visible, but cannot be edited?

class Item(models.Model):     sku = models.CharField(max_length=50)     description = models.CharField(max_length=200)     added_by = models.ForeignKey(User)   class ItemForm(ModelForm):     class Meta:         model = Item         exclude = ('added_by')  def new_item_view(request):     if request.method == 'POST':         form = ItemForm(request.POST)         # Validate and save     else:             form = ItemForm()     # Render the view 

Can class ItemForm be reused? What changes would be required in the ItemForm or Item model class? Would I need to write another class, ‘ItemUpdateForm‘, for updating the item?

def update_item_view(request):     if request.method == 'POST':         form = ItemUpdateForm(request.POST)         # Validate and save     else:         form = ItemUpdateForm() 
  • 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. 2026-05-10T21:30:33+00:00Added an answer on May 10, 2026 at 9:30 pm

    As pointed out in this answer, Django 1.9 added the Field.disabled attribute:

    The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users. Even if a user tampers with the field’s value submitted to the server, it will be ignored in favor of the value from the form’s initial data.

    With Django 1.8 and earlier, to disable entry on the widget and prevent malicious POST hacks you must scrub the input in addition to setting the readonly attribute on the form field:

    class ItemForm(ModelForm):     def __init__(self, *args, **kwargs):         super(ItemForm, self).__init__(*args, **kwargs)         instance = getattr(self, 'instance', None)         if instance and instance.pk:             self.fields['sku'].widget.attrs['readonly'] = True      def clean_sku(self):         instance = getattr(self, 'instance', None)         if instance and instance.pk:             return instance.sku         else:             return self.cleaned_data['sku'] 

    Or, replace if instance and instance.pk with another condition indicating you’re editing. You could also set the attribute disabled on the input field, instead of readonly.

    The clean_sku function will ensure that the readonly value won’t be overridden by a POST.

    Otherwise, there is no built-in Django form field which will render a value while rejecting bound input data. If this is what you desire, you should instead create a separate ModelForm that excludes the uneditable field(s), and just print them inside your template.

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

Sidebar

Ask A Question

Stats

  • Questions 82k
  • Answers 82k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do this easily with IsNull: INSERT INTO newtable… May 11, 2026 at 4:38 pm
  • Editorial Team
    Editorial Team added an answer Your problem is that the backslash has special meaning both… May 11, 2026 at 4:38 pm
  • Editorial Team
    Editorial Team added an answer If the finalizer of that class calls Dispose(), yes. If… May 11, 2026 at 4:38 pm

Related Questions

This is a follow-up on How do you change the default widget for all
I'm been a PHP developer for quite some time now but until today I've
This was fixed in Django 1.9 with form_kwargs . I have a Django Form
Is it possible to have a variable number of fields using django forms? The

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.