Am I interpreting the default behavior of the Django Admin site to not use the ModelManager's create method? If so, how would I get this behavior?
My model has a custom Model manager with bespoke create method.
Override the ModelAdmin.save_model() method:
class MyAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
if change:
obj.save()
else:
new_obj = MyModel.objects.create(name=obj.name, ...)
obj.pk = new_obj.pk
Related
I want to add a checkbox in Django Admin that is not related to a field in my model.
Depending on the value of the checkbox, I want to do some extra actions.
class DeviceAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
#if checkbox:
# do_extra_actions()
super(DeviceAdmin, self).save_model(request, obj, form, change)
How to add this checkbox in the django admin form for my model Device and get the value in save_model function ?
You can first create a ModelForm with such extra checkbox:
class DeviceModelForm(forms.ModelForm):
extra_checkbox = forms.BooleanField(required=False)
Then you plug this into the DeviceAdmin and inspect its value:
class DeviceAdmin(admin.ModelAdmin):
form = DeviceModelForm
def save_model(self, request, obj, form, change):
if form.cleaned_data['extra_checkbox']:
# do_extra_actions()
pass
return super().save_model(request, obj, form, change)
i defined a save_model in my UserAdmin to change object level permissions for users.
class UserAdmin(BaseUserAdmin):
def save_model(self, request, obj, form, change):
obj.save()
allprojects = Project.objects.all()
projects = obj.workingproject.all()
remove_perm("view_project", obj, allprojects)
assign_perm("view_project", obj, projects)
obj.save()
remove_perm and assign_perm are shortcuts from django-guardian, workingproject is a M2M field of user.
The problem: when selecting different projects and saving the permissions are not changed, but pressing the save button a second time makes the changes as wanted. What am i doing wrong?
Try this by overriding save_related
def save_related(self, request, form, formsets, change):
super(UserAdmin, self).save_related(request, form, formsets, change)
obj = form.instance
allprojects = Project.objects.all()
projects = obj.workingproject.all()
remove_perm("view_project", obj, allprojects)
assign_perm("view_project", obj, projects)
In save_related, Django is removing M2M that are already there and setting new ones. So changes doing in save_model and save will not reflect.
class MyParentModelAdmin(admin.ModelAdmin):
def save_formset(self, request, form, formset, change):
if formset.model._meta.db_table=='MyInLIneModel':
In the debugger
formset.model = {ModelBase} <class 'my app.models.MyInLineModel'>
I am able to check by formset.model._meta.db_table which is 'MyInLineModel'.
I know this is not the right way.
can any one suggest a clean way to check the model name.
I have no idea why you do this but you can check it this way:
from your_app.models import SomeModel
class MyParentModelAdmin(admin.ModelAdmin):
def save_formset(self, request, form, formset, change):
if formset.model == SomeModel:
pass
I've been trying to make the GenericTabularInline class work in a two-admin two-databases setup by inheriting from it and overriding some methods in the BaseModelAdmin class, as is done in the Django docs (https://docs.djangoproject.com/en/dev/topics/db/multi-db/), but if a child model is edited in the inline form, it always writes to the default database (I want the second admin to deal exclusively with a secondary database, models are the same for both), so I must not be overriding some method(s) or doing something wrong. Here's the class I have so far:
class MultiDBGenericTabularInline(generic.GenericTabularInline):
using = settings.SECONDARY_DATABASE
def save_model(self, request, obj, form, change):
# Tell Django to save objects to the 'other' database.
obj.save(using=self.using)
def delete_model(self, request, obj):
# Tell Django to delete objects from the 'other' database
obj.delete(using=self.using)
def queryset(self, request):
# Tell Django to look for objects on the 'other' database.
return super(MultiDBGenericTabularInline, self).queryset(request).using(self.using)
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
# Tell Django to populate ForeignKey widgets using a query
# on the 'other' database.
return super(MultiDBGenericTabularInline, self).formfield_for_foreignkey(db_field, request=request, using=self.using, **kwargs)
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
# Tell Django to populate ManyToMany widgets using a query
# on the 'other' database.
return super(MultiDBGenericTabularInline, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)
#Override these three methods; otherwise the log manager attempts
#to write to the main db and raises an exception.
def log_addition(self, request, object):
pass
def log_change(self, request, object, message):
pass
def log_deletion(self, request, object, object_repr):
pass
Any help or hints are appreciated.
I realize this is an old question, but I've stumbled across a very similar thing recently. The trick is to override the parent Model Admin's save_formset method. In my case, the solution is to do something like this:
class SomeTabularInline(admin.TabularInline):
# stuff
class MyModelAdmin(admin.ModelAdmin):
using = 'something'
inlines = (SomeTabularInline,)
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for obj in formset.deleted_objects:
obj.delete(using=self.using)
for instance in instances:
instance.save(using=self.using)
formset.save_m2m()
Note: I'm using a TabularInline instance, and not a GenericTabularInline but they both descend from InlineModelAdmin; so I'm hopeful that this would work in your case.
Source: https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset
whenever I save a model in my Admin interface, it displays the usual "successfully saved message."
However, I want to know if it's possible to customize this message because I have a situation where I want to warn the user about what he just saved and the implications of these actions.
class PlanInlineFormset(forms.models.BaseInlineFormset):
def clean(self):
### How can I detect the changes?
### (self.changed_data doesn't work because it's an inline)
### and display what he/she just changed at the top AFTER the successful save?
class PlanInline(admin.TabularInline):
model = Plan
formset = PlanInlineFormset
Django (> version 1.2) uses the messages framework for admin messages. You can add additional messages using that interface. Here's an example:
from django.contrib import messages
class SomeModelAdmin(admin.ModelAdmin):
# your normal ModelAdmin stuff goes here
def save_model(self, request, obj, form, change):
# add an additional message
messages.info(request, "Extra message here.")
super(SomeModelAdmin, self).save_model(request, obj, form, change)
To detect changes to the object being saved, you should be to override the save_model method of ModelAdmin, and compare the object the method is passed to the version currently in the database. To do this in the case of inlines, you can override the save_formset method. A possible approach might look like (untested code):
class SomeModelAdmin(admin.ModelAdmin):
# your normal ModelAdmin stuff goes here
def save_formset(self, request, form, formset, change):
if not change:
formset.save()
else:
instances = formset.save(commit=False)
for instance in instances:
try:
# if you've got multiple types of inlines
# make sure your fetching from the
# appropriate model type here
old_object = SomeOtherModel.get(id=instance.id)
except SomeOtherModel.DoesNotExist:
continue
if instance.field_x != old_object.field_x:
messages.info(request, "Something Changed")
instance.save()
formset.save_m2m()
If you're using Django 1.2 or newer, the messages framework may hold the answer.
http://docs.djangoproject.com/en/dev/ref/contrib/messages/