Login

Disable fields in oldforms admin using jQuery

Author:
schraal
Posted:
June 17, 2008
Language:
JavaScript
Version:
Not specified
Score:
0 (after 0 ratings)

This snippet shows how to disable fields in a edit page in the oldforms admin using jquery.

The idea is to add the javascript to the edit page using the js attribute of the model's Admin class. In this case jQuery and a custom javascript file are added.

The javascript sets the disabled attribute of the name field to true as soon as the document is ready.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Model:

class Placeholder(models.Model):
    name=models.CharField()
    description = models.CharField(blank=True, maxlength=200)
 
    class Admin:
        list_display = ('name', 'description')
        js = (
              '/appmedia/js/jquery.js',
              '/appmedia/js/placeholder.js',
              )

placeholder.js:

$(document).ready(function(){
   $('#id_name').attr("disabled", true);
});

More like this

  1. Django Collapsed Stacked Inlines by applecat 1 year, 9 months ago
  2. Django Collapsed Stacked Inlines by mkarajohn 3 years, 10 months ago
  3. Dynamically adding forms to a formset. OOP version. by halfnibble 9 years, 6 months ago
  4. Convert multiple select for m2m to multiple checkboxes in django admin form by abidibo 11 years, 7 months ago
  5. Django admin inline ordering - javascript only implementation by ojhilt 11 years, 11 months ago

Comments

miguelvel18 (on October 8, 2009):

when the field is disabled,web form shows a error message of validation because the field is required. The field have a default value, but shows a error message validation, help me

#

Please login first before commenting.