#22828: Model admins should return copies of its attributes
-------------------------------+--------------------------------------
     Reporter:  vzima          |                    Owner:  ericpauley
         Type:  Bug            |                   Status:  assigned
    Component:  contrib.admin  |                  Version:  master
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------

Comment (by vzima):

 I see more than a few examples.

 Example 1: You can have admin, who can edit all of the user's data, and
 people from personal department, who can't edit username and permissions.
 {{{
 #!python
 class UserAdmin(ModelAdmin):
     readonly_fields = ['uuid'] # There can be other fields, e.g.
 identifiers to some other databases
     def get_readonly_fields(self, request, obj=None):
         readonly = super(UserAdmin, self).get_readonly_fields(request,
 obj=obj)
         if is_admin(request.user):
             return readonly
         elif is_personal_dept(request.user):
             readonly += ['permissions', 'groups'] # This will edit the
 class, so it will become readonly for everybody
             return readonly
     # Methods `has_*_permission` are modified appropriately
 }}}

 Example 2: Anything that needs somebody else's approval, e.g. vacation.
 {{{
 #!python
 class VacationAdmin(ModelAdmin):
     readonly_fields = ['uuid', 'fields_for_accounting']
     def get_readonly_fields(self, request, obj=None):
         readonly = super(VacationAdmin, self).get_readonly_fields(request,
 obj=obj)
         if is_admin(request.user) or is_boss(request.user):
             return readonly
         else:
             readonly.append('approved')
             return readonly
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22828#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.9b8dc0899081e49b2bd4b9f198f69277%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to