Reputation: 91
How do I allow fields to be populated by the user at the time of object creation ("add" page) and then made read-only when accessed at "change" page?
Upvotes: 9
Views: 7865
Reputation: 476
The simplest solution I found was to override the get_readonly_fields function of ModelAdmin:
class TestAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
'''
Override to make certain fields readonly if this is a change request
'''
if obj is not None:
return self.readonly_fields + ('title',)
return self.readonly_fields
admin.site.register(TestModel, TestAdmin)
Object will be none for the add page, and an instance of your model for the change page. Edit: Please note this was tested on Django==1.2
Upvotes: 10
Reputation: 6748
There's two thing to address in your question.
Doesn't exist as is in Django, but you can implement it yourself, and this blog post can help.
I guess you're looking for a solution in the admin site context (otherwise, just use 2 different forms in your views).
You could eventually override add_view
or change_view
in your ModelAdmin
and use a different form in one of the view, but I'm afraid you will end up with an awful load of duplicated code.
Another solution I can think of, is a form that will modify its fields upon instantiation, when passed an instance
parameter (ie: an edit case). Assuming you have a ReadOnlyField
class, that would give you something like:
class MyModelAdminForm(forms.ModelForm):
class Meta:
model = Stuff
def __init__(self, *args, **kwargs):
super(MyModelAdminForm, self).__init__(*args, **kwargs)
if kwargs.get('instance') is not None:
self.fields['title'] = ReadOnlyField()
In here, the field title
in the model Stuff
will be read-only on the change page of the admin site, but editable on the creation form.
Hope that helps.
Upvotes: 3
Reputation: 776
You can edit that model's save method to handle such a requirement. For example, you can check if the field already contains some value, if it does, ignore the new value.
Upvotes: 2
Reputation: 449
One option is to override or replace the change_form template for that specific model.
Upvotes: 1