Reputation: 8985
I am trying to use an inline
in UserAdmin
in admin.py
I am looking for a way to modify the fields of that inline
based on the object.
class ProfileInline(admin.StackedInline):
model = UserProfile
filter_horizontal = ('user_markets',)
fk_name = 'user'
max_num = 1
can_delete = False
fields = ('email_role', )
verbose_name_plural = 'Profile'
class UserAdmin(UserAdmin):
list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', roles, login)
list_filter = ('groups',)
inlines = (ProfileInline,)
Here I need to modify ProfileInline.fields = ('department','email_role')
if the user belongs to the Sales
Group
, else whatever.
I need a way to access the user Object
and update the fields
.
Upvotes: 1
Views: 1243
Reputation: 1137
class ProfileInline(admin.StackedInline):
model = UserProfile
filter_horizontal = ('user_markets',)
fk_name = 'user'
max_num = 1
can_delete = False
fields = ('email_role', )
verbose_name_plural = 'Profile'
def get_fieldsets(self, request, obj=None):
fieldsets = super(ProfileInline, self).get_fieldsets(request, obj)
# fieldsets[0][1]['fields'].remove('email_role')
fieldsets[0][1]['fields'] = ('department', 'email_role')
return fieldsets
get_fieldsets
method is your solution. You have request
object so request.user
also.
Upvotes: 2