views:

239

answers:

3

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?

+1  A: 

One option is to override or replace the change_form template for that specific model.

davidfischer
note that changing only the template is *not* secure. An user can still submit the field in a forged _POST_ that will get accepted on the server-side.
Clément
A: 

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.

Luiz C.
A: 

There's two thing to address in your question.

1. Read-only form fields

Doesn't exist as is in Django, but you can implement it yourself, and this blog post can help.

2. Different form for add/change

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.

Clément