django / code.djangoproject.com

Configuration for Django's Trac instance (code.djangoproject.com)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Save as new" with read-only fields

nicolazilio opened this issue · comments

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.

This is the issue tracker for the code.djangoproject.com website. Please see TicketClosingReasons/UseSupportChannels for ways to get help with Django usage. If you confirm a bug, report it at code.djangoproject.com.