python - How to make a field non modifiable on edit page but able to create on add page -


i have scenario given below:

in models.py

class room(models.model):    pop = models.foreignkey('pop', verbose_name="pop",                            help_text="pop room belongs to", null=true,                            blank=true)    .... other fields ....   class pop(models.model):     .... fields .... 

in admin.py

class roomadmin(admin.modeladmin):     search_fields = [..some fields...]     list_diplay = (pop, ....)     ..... other configuration ..... 

since room has foreign key relationship pop, possible associate multiple rooms 1 pop. have t o implement this.

  1. when user clicks on "add pop" link django admin page, user should allowed associate multiple room objects pop object. once room objects have been associated pop, can neither edited nor can associated pop edit screen. possible achieve this?

i have these possible solutions don't know how implement these ideas in django or if these ideas viable:

  1. css magic. can associate css id pop field can enabled edit screens. not sure how implement this

  2. django's admin.py, if exclude pop field edit screen, might work.

  3. modify templates can support non editable fields.

hoping help.

option 2 best. can override get_form method in roomadmin class exclude pop field when there object edit:

def get_form(self, request, obj=none, **kwargs):     # if there's object , has been saved     if obj not none , obj.id not none:         # add 'pop' list of fields exclude         exclude = list(kwargs.get('exclude', []))         exclude.append('pop')         kwargs['exclude'] = exclude     return super(roomadmin, self).get_form(request, obj, kwargs) 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -