Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30373 closed Bug (wontfix)

"Save as new" with read-only fields.

Reported by: Nicola Zilio Owned by: nobody
Component: contrib.admin Version: dev
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 want to implement the "Save as new" feature in Django's admin for a model such as this one:

class Plasmid (models.Model):
    name = models.CharField("Name", max_length = 255, blank=False)
    other_name = models.CharField("Other Name", max_length = 255, blank=True)
    selection = models.CharField("Selection", max_length = 50, blank=False)
    created_by = models.ForeignKey(User)

In the admin, if the user who requests a Plasmid object is NOT the same as the one who created it, some of the above-shown fields are set as read-only. If the user is the same, they are all editable. For example:

class PlasmidPage(admin.ModelAdmin):

    def get_readonly_fields(self, request, obj=None):

        if obj:
            if not request.user == obj.created_by:
                return ['name', 'created_by',]
            else:
                return ['created_by',]
        else:
            return []

    def change_view(self,request,object_id,extra_context=None):

        self.fields = ('name', 'other_name', 'selection', 'created_by',)
        return super(PlasmidPage,self).change_view(request,object_id)

The issue I have is that when a field is read-only and a user hits the "Save as new" button, the value of that field is not 'transferred' to the new object. On the other hand, the values of fields that are editable (not read-only) are transferred.

I don't understand if this is a bug or a (security?) feature. In either case does anybody why, or how I could solve this problem? I want to transfer the values of both read-only and non-read-only fields to the new object.

Change History (2)

comment:1 by Mariusz Felisiak, 5 years ago

Resolution: wontfix
Status: newclosed
Summary: "Save as new" with read-only fields"Save as new" with read-only fields.
Version: 1.11master

Thanks for the report, however there is not much we can do. Read-only fields don't have form fields that's why submit doesn't take them into account. You can probably find some workaround (e.g. with hidden fields), please use one of support channels.

comment:2 by Nicola Zilio, 5 years ago

Good to know. I should say that I tried to circumvent this problem by disabling the fields, rather than setting them as readonly, but unfortunately, the result is the same.

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