Add link to ModelAdmin - django

I have a ModelForm:
class SomeModelForm(forms.ModelForm):
class Meta:
model = SomeModel
def __init__(self, *args, **kwargs):
super(ApiBackendConfigForm, self).__init__(*args, **kwargs)
if kwargs['instance'].name == u'Some_name':
self.fields['method_config'] = forms.URLField()
and ModelAdmin:
class SomeAdmin(admin.ModelAdmin):
form = SomeModelForm
list_display = ('name', 'alias', 'is_enabled', )
list_editable = ('is_enabled', )
readonly_fields = ('name', 'alias', )
First question, method_config field is not displayed. I know, that it's not in list_display, but if I add it to list_display, then it causes an error.
And second main question: How can I add some link to other ModelAdmin?

Modifing self.fields might not be thread-safe. This means that if you modify self.fields on first request, all other requests will get that modified version.
Check ModelAdmin.get_fields(request, obj=None) method for changing which fields to display on the fly. Works on forms in changeview. If you want to display custom field in changelist view, just modify list_display wih method name instead of field.
For example:
class SomeAdmin(admin.ModelAdmin):
list_display = ('name', 'alias', 'is_enabled', 'show_method_config', )
list_editable = ('is_enabled', )
readonly_fields = ('name', 'alias', )
def show_method_config(self,obj):
return getattr(obj,"method_config","") if obj.name == u"Some_name" else ""
show_method_config.short_description = _(u"Method config")
show_method_config.admin_order_field = "method_config"
show_method_config.allow_tags = True
def get_fields(self, request, obj=None):
fields = super(SomeAdmin, self).get_fields(request, obj)
if obj and obj.name == u"Some_name":
fields.append("method_config")
return fields

Related

Django admin dynamically add to list_display and create ModelAdmin methods

I'd like to dynamically include some extra fields in the list_display of the admin for one of my models. I plan on overriding get_list_display to append a string representing a ModelAdmin method but how can I dynamically create the ModelAdmin methods?
In the example below if get_list_display returns [..., 'module_1', 'module_2'] that means I need methods for module_1 and module_2 as they're not model fields.
class Type(models.Model):
...
modules = models.ManyToManyField(Module)
class User(AbstractBaseUser):
...
type = models.ForeignKey(Type)
class UserModuleRecord(User):
class Meta:
proxy=True
#admin.register(UserModuleRecord)
class UserModuleRecordAdmin(admin.ModelAdmin):
list_display = ['id', 'first_name', 'last_name']
def get_queryset(self, request):
return (
super().get_queryset(request)
.annotate_the_additional_list_display_values()
)
def get_list_display(self, request):
list_display = super().get_list_display(request)
modules = Module.objects.all()
for module in modules:
list_display.append('module_%s' % module.id)
return list_display
Additionally is it possible to create something similar to how get_FOO_display works so there's only one admin method required?
UPDATE
I think I'm close with the following but am getting an error TypeError: 'partialmethod' object is not callable
from functools import partialmethod
def get_list_display(self, request):
list_display = super().get_list_display(request)
modules = Module.objects.all()
for module in modules:
attr_name = 'module_%s' % module.id
list_display.append(attr_name)
if not hasattr(self, attr_name):
setattr(self, attr_name, partialmethod(
self._module_id, field=attr_name
))
return list_display
def _module_id(self, obj, field=''):
return getattr(obj, field, '')
The following makes use of functools.partial to return a partial object which behaves like a function when called - similar to Django's get_FOO_display().
#admin.register(models.EmployeeTrainingRecord)
class EmployeeTrainingRecordAdmin(admin.ModelAdmin):
ordering = ('email',)
list_display = [
'id',
'first_name',
'last_name',
]
def get_queryset(self, request):
return (
super().get_queryset(request)
.annotate_training_dates()
)
def get_list_display(self, request):
list_display = super().get_list_display(request)
training_modules = models.Training.objects.all()
for training in training_modules:
attr_name = 'training_%s' % training.id
if not hasattr(self, attr_name):
list_display.append(attr_name)
func = partial(self._get_training_id, field=attr_name)
func.short_description = training.name
setattr(self, attr_name, func)
return list_display
#staticmethod
def _get_training_id(obj, field=''):
return getattr(obj, field, '')
If I'm understanding your question correctly I think you're half way to the answer already.
As you know, to add anything custom to the list_display fields that isn't already a model field you need to implement it with a method on your ModelAdmin instance.
You can dynamically create field methods on the ModelAdmin instance using python's setattr and a wrapped function and doing it when the instance is initialised.
For example you could create a series of methods on your instance this way:
def make_dynamic_field(*args):
def dynamic_field(self, obj):
i = args[0]
return f'{obj.first_name} {i}'
return dynamic_field
#admin.register(UserModuleRecord)
class UserModuleRecordAdmin(admin.ModelAdmin):
...
list_display = ['id', 'first_name', 'last_name', 'foo_0', 'foo_1']
def __init__(self, model, admin_site):
super().__init__(model, admin_site)
for i in range(2):
setattr(self, f'foo_{i}', make_dynamic_field(i))
I have used the index variable i as an example but you could potentially inject any variable into your dynamic field method this way.

How to hide some fields in django-admin?

class Book(models.Model):
title = models.CharField(..., null=True)
type = models.CharField(...)
author = models.CharField(...)
I have a simple class in models.py. In admin I would like to hide title of the book (in book details form) when type of the saved book is 1.
How do this in a simplest way?
For Django > 1.8 one can directly set the fields to be excluded in admin:
class PostCodesAdmin(admin.ModelAdmin):
exclude = ('pcname',)
Hidden fields are directly defined in Django's ORM by setting the Field attribute: editable = False
e.g.
class PostCodes(models.Model):
gisid = models.IntegerField(primary_key=True)
pcname = models.CharField(max_length=32, db_index=True, editable=False)
...
However, setting or changing the model's fields directly may not always be possible or advantegous. In principle the following admin.py setup could work, but won't since exclude is an InlineModelAdmin option.
class PostCodesAdmin(admin.ModelAdmin):
exclude = ('pcname',)
....
A solution working at least in Django 1.4 (and likely later version numbers) is:
class PostCodesAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super(PostCodesAdmin, self).get_form(request, obj, **kwargs)
del form.base_fields['enable_comments']
return form
For the admin list-view of the items, it suffices to simply leave out fields not required:
e.g.
class PostCodesAdmin(admin.ModelAdmin):
list_display = ('id', 'gisid', 'title', )
You are to create admin.py in your module (probably book)
class BookAdmin(admin.ModelAdmin):
list_display = ("pk", "get_title_or_nothing")
In Book class:
class Book:
...
def get_title_or_nothing(self):
if self.type == WEIRD_TYPE:
return ""
return self.title
UPDATED:
class BookAdmin(admin.ModelAdmin):
list_display = ("pk", "get_title_or_nothing")
def get_form(self, request, obj=None, **kwargs):
if obj.type == "1":
self.exclude = ("title", )
form = super(BookAdmin, self).get_form(request, obj, **kwargs)
return form
I tried to override get_form() function but some mix up errors occur when I switch in different records. I found there is a get_exclude() function we can override.
Use:
class BookAdmin(admin.ModelAdmin):
def get_exclude(self, request, obj=None):
if obj and obj.type == "1":
# When you create new data the obj is None
return ("title", )
return super().get_exclude(request, obj)
class BookAdmin(admin.ModelAdmin):
exclude = ("fieldname",) # hide fields which you want
Apropos #Lorenz #mrts answer
with Django 2.1 I found that exclude does not work if the field is already specified via fields = .
In that case you may use
self.fields.remove('title')
fields will have to be defined as a list [] for this to work
If you want to maintain the value in the form (for example set a value, i.e. user, based on the request) and hide the field, you can change the widget to forms.HiddenInput():
from django import forms
...
def get_form(self, request, obj=None, **kwargs):
"""Set defaults based on request user"""
# update user field with logged user as default
form = super().get_form(request, obj, **kwargs)
form.base_fields["user"].initial = request.user.id
form.base_fields["user"].widget = forms.HiddenInput()
return form
Here is a working example
class BookAdmin(admin.ModelAdmin):
def get_fieldsets(self, request, obj):
if obj is None:
return [
(
None,
{'fields': ('type', 'description',)}
)
]
elif request.user.is_superuser:
return [
(
None,
{'fields': ('type', 'status', 'author', 'store', 'description',)}
)
]
else:
return [
(
None,
{'fields': ('type', 'date', 'author', 'store',)}
)
]

Get form fields list [duplicate]

I've tried various methods to achieve this.
I decided against overriding formfield_for_dbfield as it doesn't get a copy of the request object and I was hoping to avoid the thread_locals hack.
I settled on overriding get_form in my ModelAdmin class and tried the following:
class PageOptions(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
if request.user.is_superuser:
self.fieldsets = ((None, {'fields': ('title','name',),}),)
else:
self.fieldsets = ((None, {'fields': ('title',),}),)
return super(PageOptions,self).get_form(request, obj=None, **kwargs)
When I print fieldsets or declared_fieldsets from within get_form I get None (or whatever I set as an initial value in PageOptions).
Why doesn't this work and is there a better way to do this?
I have some sample code from a recent project of mine that I believe may help you. In this example, super users can edit every field, while everyone else has the "description" field excluded.
Note that I think it's expected that you return a Form class from get_form, which could be why yours was not working quite right.
Here's the example:
class EventForm(forms.ModelForm):
class Meta:
model = models.Event
exclude = ['description',]
class EventAdminForm(forms.ModelForm):
class Meta:
model = models.Event
class EventAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
if request.user.is_superuser:
return EventAdminForm
else:
return EventForm
admin.site.register(models.Event, EventAdmin)
I have no idea why printing the property doesn't give you want you just assigned (I guess may be that depends on where you print, exactly), but try overriding get_fieldsets instead. The base implementation looks like this:
def get_fieldsets(self, request, obj=None):
if self.declared_fieldsets:
return self.declared_fieldsets
form = self.get_formset(request).form
return [(None, {'fields': form.base_fields.keys()})]
I.e. you should be able to just return your tuples.
EDIT by andybak. 4 years on and I found my own question again when trying to do something similar on another project. This time I went with this approach although modified slightly to avoid having to repeat fieldsets definition:
def get_fieldsets(self, request, obj=None):
# Add 'item_type' on add forms and remove it on changeforms.
fieldsets = super(ItemAdmin, self).get_fieldsets(request, obj)
if not obj: # this is an add form
if 'item_type' not in fieldsets[0][1]['fields']:
fieldsets[0][1]['fields'] += ('item_type',)
else: # this is a change form
fieldsets[0][1]['fields'] = tuple(x for x in fieldsets[0][1]['fields'] if x!='item_type')
return fieldsets
This is my solution:
class MyModelAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
if request.user.is_superuser:
self.exclude = ()
else:
self.exclude = ('field_to_exclude',)
return super(MyModelAdmin, self).get_form(request, obj=None, **kwargs)
Hope can help
For creating customized admin forms we have defined a new class which can be used as mixin. The approach is quite flexible:
ModelAdmin: define a fieldset containing all fields
ModelForm: narrow the fields being shown
FlexibleModelAdmin: overriding get_fieldsets-method of ModelAdmin; returns a reduced fieldset that only contains the fields defined in the admin form
class FlexibleModelAdmin(object):
'''
adds the possibility to use a fieldset as template for the generated form
this class should be used as mix-in
'''
def _filterFieldset(self, proposed, form):
'''
remove fields from a fieldset that do not
occur in form itself.
'''
allnewfields = []
fields = form.base_fields.keys()
fieldset = []
for fsname, fdict in proposed:
newfields = []
for field in fdict.get('fields'):
if field in fields:
newfields.append(field)
allnewfields.extend(newfields)
if newfields:
newentry = {'fields': newfields}
fieldset.append([fsname, newentry])
# nice solution but sets are not ordered ;)
# don't forget fields that are in a form but were forgotten
# in fieldset template
lostfields = list(set(fields).difference(allnewfields))
if len(lostfields):
fieldset.append(['lost in space', {'fields': lostfields}])
return fieldset
def get_fieldsets(self, request, obj=None):
'''
Hook for specifying fieldsets for the add form.
'''
if hasattr(self, 'fieldsets_proposed'):
form = self.get_form(request, obj)
return self._filterFieldset(self.fieldsets_proposed, form)
else:
return super(FlexibleModelAdmin, self).get_fieldsets(request, obj)
In the admin model you define fieldsets_proposed which serves as template and contains all fields.
class ReservationAdmin(FlexibleModelAdmin, admin.ModelAdmin):
list_display = ['id', 'displayFullName']
list_display_links = ['id', 'displayFullName']
date_hierarchy = 'reservation_start'
ordering = ['-reservation_start', 'vehicle']
exclude = ['last_modified_by']
# considered by FlexibleModelAdmin as template
fieldsets_proposed = (
(_('General'), {
'fields': ('vehicle', 'reservation_start', 'reservation_end', 'purpose') # 'added_by'
}),
(_('Report'), {
'fields': ('mileage')
}),
(_('Status'), {
'fields': ('active', 'editable')
}),
(_('Notes'), {
'fields': ('note')
}),
)
....
def get_form(self, request, obj=None, **kwargs):
'''
set the form depending on the role of the user for the particular group
'''
if request.user.is_superuser:
self.form = ReservationAdminForm
else:
self.form = ReservationUserForm
return super(ReservationAdmin, self).get_form(request, obj, **kwargs)
admin.site.register(Reservation, ReservationAdmin)
In your model forms you can now define the fields to be excluded/included. get_fieldset() of the mixin-class makes sure that only the fields defined in the form are being returned.
class ReservationAdminForm(ModelForm):
class Meta:
model = Reservation
exclude = ('added_by', 'last_modified_by')
class ReservationUserForm(BaseReservationForm):
class Meta:
model = Reservation
fields = ('vehicle', 'reservation_start', 'reservation_end', 'purpose', 'note')
Don't change the value of self attributes because it's not thread-safe. You need to use whatever hooks to override those values.
In my case, with Django 2.1 you could do the following
In forms.py
class ObjectAddForm(forms.ModelForm):
class Meta:
model = Object
exclude = []
class ObjectChangeForm(forms.ModelForm):
class Meta:
model = Object
exclude = []
And then in the admin.py
from your.app import ObjectAddForm, ObjectChangeForm
class ObjectAdmin(admin.ModelAdmin):
....
def get_form(self, request, obj=None, **kwargs):
if obj is None:
kwargs['form'] = ObjectAddForm
else:
kwargs['form'] = ObjectChangeForm
return super().get_form(request, obj, **kwargs)
You can use get_fields or get_fieldset methods for that purpose
You could make fieldsetsand form properties and have them emit signals to get the desired forms/fieldsets.

Django: How to remove fields from the admin form for specific users?

My admin looks like this (with no exclude variable):
class MovieAdmin(models.ModelAdmin)
fields = ('name', 'slug', 'imdb_link', 'start', 'finish', 'added_by')
list_display = ('name', 'finish', 'added_by')
list_filter = ('finish',)
ordering = ('-finish',)
prepopulated_fields = {'slug': ('name',)}
form = MovieAdminForm
def get_form(self, request, obj=None, **kwargs):
form = super(MovieAdmin, self).get_form(request, obj, **kwargs)
form.current_user = request.user
return form
admin.site.register(Movie, MovieAdmin)
The form:
class MovieAdminForm(forms.ModelForm):
class Meta:
model = Movie
def save(self, commit=False):
instance = super(MovieAdminForm, self).save(commit=commit)
if not instance.pk and not self.current_user.is_superuser:
if not self.current_user.profile.is_manager:
instance.added_by = self.current_user.profile
instance.save()
return instance
I'm trying to remove the added_by field for users since I'd prefer to populate that from the session. I've tried methods from the following:
Django admin - remove field if editing an object
Remove fields from ModelForm
http://www.mdgart.com/2010/04/08/django-admin-how-to-hide-fields-in-a-form-for-certain-users-that-are-not-superusers/
However with each one I get: KeyError while rendering: Key 'added_by' not found in Form. It seems I need to remove the field earlier in the form rendering process but I'm stuck on where to do this.
So how can I exclude the added_by field for normal users?
You're probably getting that error when list_display is evaluated. You can't show a field that's excluded. The version with added_by removed also needs a corresponding list_display.
def get_form(self, request, obj=None, **kwargs):
current_user = request.user
if not current_user.profile.is_manager:
self.exclude = ('added_by',)
self.list_display = ('name', 'finish')
form = super(MovieAdmin, self).get_form(request, obj, **kwargs)
form.current_user = current_user
return form

Django - Overriding get_form to customize admin forms based on request

I've tried various methods to achieve this.
I decided against overriding formfield_for_dbfield as it doesn't get a copy of the request object and I was hoping to avoid the thread_locals hack.
I settled on overriding get_form in my ModelAdmin class and tried the following:
class PageOptions(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
if request.user.is_superuser:
self.fieldsets = ((None, {'fields': ('title','name',),}),)
else:
self.fieldsets = ((None, {'fields': ('title',),}),)
return super(PageOptions,self).get_form(request, obj=None, **kwargs)
When I print fieldsets or declared_fieldsets from within get_form I get None (or whatever I set as an initial value in PageOptions).
Why doesn't this work and is there a better way to do this?
I have some sample code from a recent project of mine that I believe may help you. In this example, super users can edit every field, while everyone else has the "description" field excluded.
Note that I think it's expected that you return a Form class from get_form, which could be why yours was not working quite right.
Here's the example:
class EventForm(forms.ModelForm):
class Meta:
model = models.Event
exclude = ['description',]
class EventAdminForm(forms.ModelForm):
class Meta:
model = models.Event
class EventAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
if request.user.is_superuser:
return EventAdminForm
else:
return EventForm
admin.site.register(models.Event, EventAdmin)
I have no idea why printing the property doesn't give you want you just assigned (I guess may be that depends on where you print, exactly), but try overriding get_fieldsets instead. The base implementation looks like this:
def get_fieldsets(self, request, obj=None):
if self.declared_fieldsets:
return self.declared_fieldsets
form = self.get_formset(request).form
return [(None, {'fields': form.base_fields.keys()})]
I.e. you should be able to just return your tuples.
EDIT by andybak. 4 years on and I found my own question again when trying to do something similar on another project. This time I went with this approach although modified slightly to avoid having to repeat fieldsets definition:
def get_fieldsets(self, request, obj=None):
# Add 'item_type' on add forms and remove it on changeforms.
fieldsets = super(ItemAdmin, self).get_fieldsets(request, obj)
if not obj: # this is an add form
if 'item_type' not in fieldsets[0][1]['fields']:
fieldsets[0][1]['fields'] += ('item_type',)
else: # this is a change form
fieldsets[0][1]['fields'] = tuple(x for x in fieldsets[0][1]['fields'] if x!='item_type')
return fieldsets
This is my solution:
class MyModelAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
if request.user.is_superuser:
self.exclude = ()
else:
self.exclude = ('field_to_exclude',)
return super(MyModelAdmin, self).get_form(request, obj=None, **kwargs)
Hope can help
For creating customized admin forms we have defined a new class which can be used as mixin. The approach is quite flexible:
ModelAdmin: define a fieldset containing all fields
ModelForm: narrow the fields being shown
FlexibleModelAdmin: overriding get_fieldsets-method of ModelAdmin; returns a reduced fieldset that only contains the fields defined in the admin form
class FlexibleModelAdmin(object):
'''
adds the possibility to use a fieldset as template for the generated form
this class should be used as mix-in
'''
def _filterFieldset(self, proposed, form):
'''
remove fields from a fieldset that do not
occur in form itself.
'''
allnewfields = []
fields = form.base_fields.keys()
fieldset = []
for fsname, fdict in proposed:
newfields = []
for field in fdict.get('fields'):
if field in fields:
newfields.append(field)
allnewfields.extend(newfields)
if newfields:
newentry = {'fields': newfields}
fieldset.append([fsname, newentry])
# nice solution but sets are not ordered ;)
# don't forget fields that are in a form but were forgotten
# in fieldset template
lostfields = list(set(fields).difference(allnewfields))
if len(lostfields):
fieldset.append(['lost in space', {'fields': lostfields}])
return fieldset
def get_fieldsets(self, request, obj=None):
'''
Hook for specifying fieldsets for the add form.
'''
if hasattr(self, 'fieldsets_proposed'):
form = self.get_form(request, obj)
return self._filterFieldset(self.fieldsets_proposed, form)
else:
return super(FlexibleModelAdmin, self).get_fieldsets(request, obj)
In the admin model you define fieldsets_proposed which serves as template and contains all fields.
class ReservationAdmin(FlexibleModelAdmin, admin.ModelAdmin):
list_display = ['id', 'displayFullName']
list_display_links = ['id', 'displayFullName']
date_hierarchy = 'reservation_start'
ordering = ['-reservation_start', 'vehicle']
exclude = ['last_modified_by']
# considered by FlexibleModelAdmin as template
fieldsets_proposed = (
(_('General'), {
'fields': ('vehicle', 'reservation_start', 'reservation_end', 'purpose') # 'added_by'
}),
(_('Report'), {
'fields': ('mileage')
}),
(_('Status'), {
'fields': ('active', 'editable')
}),
(_('Notes'), {
'fields': ('note')
}),
)
....
def get_form(self, request, obj=None, **kwargs):
'''
set the form depending on the role of the user for the particular group
'''
if request.user.is_superuser:
self.form = ReservationAdminForm
else:
self.form = ReservationUserForm
return super(ReservationAdmin, self).get_form(request, obj, **kwargs)
admin.site.register(Reservation, ReservationAdmin)
In your model forms you can now define the fields to be excluded/included. get_fieldset() of the mixin-class makes sure that only the fields defined in the form are being returned.
class ReservationAdminForm(ModelForm):
class Meta:
model = Reservation
exclude = ('added_by', 'last_modified_by')
class ReservationUserForm(BaseReservationForm):
class Meta:
model = Reservation
fields = ('vehicle', 'reservation_start', 'reservation_end', 'purpose', 'note')
Don't change the value of self attributes because it's not thread-safe. You need to use whatever hooks to override those values.
In my case, with Django 2.1 you could do the following
In forms.py
class ObjectAddForm(forms.ModelForm):
class Meta:
model = Object
exclude = []
class ObjectChangeForm(forms.ModelForm):
class Meta:
model = Object
exclude = []
And then in the admin.py
from your.app import ObjectAddForm, ObjectChangeForm
class ObjectAdmin(admin.ModelAdmin):
....
def get_form(self, request, obj=None, **kwargs):
if obj is None:
kwargs['form'] = ObjectAddForm
else:
kwargs['form'] = ObjectChangeForm
return super().get_form(request, obj, **kwargs)
You can use get_fields or get_fieldset methods for that purpose
You could make fieldsetsand form properties and have them emit signals to get the desired forms/fieldsets.