user3541631
user3541631

Reputation: 4008

Dynamic read-only field in Django Admin

In Django Admin for a Model I want all fields to be:

  1. editable on creation
  2. some of them on updating ( based on the instance fields values on creation).

For example:

2-1. If attribute a has a value, the fields corresponding to attributes c and b to be readonly

2-2. If attributes are empty after creation, should not be editable on updating

I know that for normal forms there is the Field disabled attribute.

I know I need to overwrite Admin form, but I don't have an idea, to know is created or update when form is initialized.

Usually I get the value using clean(), but here I need to get them on initialization in case of updates.

Upvotes: 0

Views: 1297

Answers (1)

Dan Moica
Dan Moica

Reputation: 274

So it is like this:

  1. You can create custom FORMS see here https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form

  2. After that you can add your logic of which form to use by overriding the get_form method. see here https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_form

However you need to make sure your DB will accept the partially submitted data. You can DROP NULL on the specific columns.

Upvotes: 2

Related Questions