Opened 10 years ago

Closed 10 years ago

#21464 closed Bug (invalid)

readonly_fields is not resetted correctly

Reported by: fiomtec@… Owned by: nobody
Component: contrib.admin Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a model with three fields, one is a FileField, two I just complete myself on POST. The next time you see this model, every field is read only.

This is what I do in the modelAdmin:

    def get_form(self, request, obj=None, **kwargs):
        if obj:
            self.fields = ('tipo', 'fecha', 'fichero')
            self.readonly_fields = ('tipo', 'fecha', 'fichero')
        else:
            self.fields = ('fichero',)

Problem is: I create a new object, I enter and see this object, and when I try to create a new one, readonly_fields is still as it was in the first object, i.e., I need to do this for the form to allow me to upload a new file:

    def get_form(self, request, obj=None, **kwargs):
        if obj:
            self.fields = ('tipo', 'fecha', 'fichero')
            self.readonly_fields = ('tipo', 'fecha', 'fichero')
        else:
            self.fields = ('fichero',)
            self.readonly_fields = ()

Change History (1)

comment:1 by alasdair, 10 years ago

Resolution: invalid
Status: newclosed

readonly_fields is a class variable, it should not be changed from request to request.

There is a get_readonly_fields hook that you could use instead. Please use the support channels if you need more help with this.

wiki:TicketClosingReasons/UseSupportChannels

Note: See TracTickets for help on using tickets.
Back to Top