#13005: We need a way to allow for request/user specific default values in the
admin fields
----------------------------------+-----------------------------------------
 Reporter:  hejsan                |       Owner:  nobody    
   Status:  new                   |   Milestone:            
Component:  django.contrib.admin  |     Version:  1.2-beta  
 Keywords:                        |       Stage:  Unreviewed
Has_patch:  0                     |  
----------------------------------+-----------------------------------------
 I propose a new function in line with the get_readonly_fields(self,
 request, obj=None) function in the ModelAdmin class.

 It could have the following signature:

 {{{
 def default_value_for_formfield(self, request, field_name, obj=None)
 }}}
 And by default return None (or "") or in the case of a ModelForm, it
 should of return the default value of the related Model.

 This is usefull because it could then be overridden in our ModelAdmin
 subclass to return different values dependant on for example the
 permissions of the currently logged in user:
 {{{
 def default_value_for_formfield(self, request, field_name, obj=None)
     super(NewsArticleAdmin, self).default_value_for_formfield(self,
 request, field_name, obj)
     user = request.user
     if not user.is_superuser:
         if 'categories' == field_name:
             if not user.has_perm('news.choose_category'):
                 return PK_OF_SOME_CATEGORY
         if 'authors' == field_name:
             if not user.has_perm('news.choose_author'):
                 # Can only mark him/herself as author
                 return user.pk
     return super(NewsArticleAdmin, self).default_value_for_formfield(self,
 request, field_name, obj)

 }}}

 I know that this functionality could be made happen using the save_model()
 method of the ModelAdmin class, but this way is still prettier, it looks
 better to the end user to have the actual name of the category displayed
 instead of (None) in the case of readonly fields and reliefs any
 ambiguity.

 This makes the new get_readonly_fields() method all the more usefull.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/13005>
Django <http://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 post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to