Opened 15 years ago
Closed 15 years ago
#13005 closed (wontfix)
We need a way to allow for request/user specific default values in the admin fields
Reported by: | hejsan | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 1.2-beta |
Severity: | 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 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.
I'm going to close this wontfix; it's a bit of an edge case, and one you can already hit by overriding get_form() and returning a form class definition whose default values are programmaticaly derived