Trouble overriding both ModelAdmin.save_model() and ModelForm.save() - django

I want to override a ModelForm's save() function so that it updates a field on the model if the user pressed a particular submit button. I also want to check through some other fields and update their values, and I've done this on the ModelAdmin's save_model() function. However, the save_model() function is being passed None for the object. If I comment out the form's save() function, then the save_model() function works as expected.
Is there an issue with overriding both, or have I made a mistake somewhere?
Here's a minimal example:
admin.py:
class TestAdmin(admin.ModelAdmin):
form = TestForm
def save_model(self, request, obj, form, change):
print 'test'
super(PostAdmin, self).save_model(request, obj, form, change)
admin.site.register(TestModel, TestAdmin)
forms.py:
class TestForm(forms.ModelForm):
class Meta:
model = TestModel
def save(self, force_insert=False, force_update=False, commit=True):
print 'test'
super(TestForm, self).save(commit=True)

Your ModelForm needs to return the instance.
As far as I remember, just prior to save_model, the admin does a save(commit=False) and passes the unsaved instance to save_model. If you don't return anything, save() == None.
return super(CategoryForm, self).save(commit=True)

If you are overriding the ModelAdmin.save_model, and not calling the super().save_model (in your example you are, I can see.), you should explicitly call the form.save().
If you are calling the ModelForm.save somehow, through super or explicit calling, I don't see why it wouldn't work; but I can tell you that if I were to override the save_model, my preference would be to limit myself to overriding the Model.save().

Related

Create method in forms.Form

My questions is, can I create methods in a form?
I want to create a method in a UserChangeForm it should control some things.
And how can I call this method then in the save() if commit?
In the forms.form I must create a save() too?
I tried this:
class UserChangeForm(forms.Form):
#fields for the form
def create_club_contact(self):
user = super(UserChangeForm, self).save(commit=False)
if information is None:
#create clubcontact
else:
#update clubcontact
return clubcontact
def clean_password2(self):
#check password
return password2
def save(self):
data = self.cleaned_data
user = TennisClub(link=data['link'], name=data['name'])
user.save()
contact = self.create_club_contact()
user.club_contact = contact
user.save()
On this way I'm always getting this Error
'super' object has no attribute 'save'
I think the forms.Form needs a save() method, but I don't know, how to do it right.
In the docs, I have not seen such a example.
Thanks for helping.
class MyForm(forms.Form):
def __init__(self):
self.member = False
# example of a method with return statement
def my_method(self):
return something
# example for a void method, no return value
def another_method(self):
self.member = True
def save(self):
my_variable = self.my_method()
self.another_method()
# continue with your logic
EDIT:
I think you don't have the problem with creating methods and calling them. The above is probably clear to you. But it seems that you have another problem, but you can't question it right.
Your form inherits from Form. The parent class Form has a method save() and you want to override it.
Put this in the end of your method save() and try again:
return super(UserChangeForm, self).save()

super() save method on Django auth user's user change form

I am trying to edit django.contrib.auth.forms.UserChangeForm. Basically, auth_user's user edit page.
https://github.com/django/django/blob/master/django/contrib/auth/forms.py
According to source code, the form does not have a save() method, so it should inherit from forms.ModelForm right?
For full code, see here
class MyUserAdminForm(forms.ModelForm):
class Meta:
model = User
def __init__(self, *args, **kwargs):
super(MyUserAdminForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.id: # username and user id
... the rest of the __init__ is setting readonly fields
.... some clean methods .....
def save(self, *args, **kwargs):
kwargs['commit'] = True
user = super(MyUserAdminForm, self).save(*args, **kwargs)
print user.username
print 'done'
return user
When I hit save, it said 'UserForm' object has no attribute 'save_m2m'. I've googled quite a bit, and tried to use add() but didn't work. What's causing this behaviour?
The thing is: the two print statements are printed. But the value never saved into database. I thought that the 2nd line would have saved once already.
Thanks
Remove the kwargs['commit'] = True line and see what happen.
Django Admin would invoke form.save_m2m(), which is hooked to the form when commit is False, here. The unconditional overriding of kwargs['commit'] = True would break the setattr of save_m2m() to form thus no attribute error is raised. The actual affected logic is here:
def save_form(self, request, form, change):
"""
Given a ModelForm return an unsaved instance. ``change`` is True if
the object is being changed, and False if it's being added.
"""
return form.save(commit=False)
You could find out that your version of form.save() overriding commit=False to commit=True unconditionally, thus Django Admin fails to continue as it believes form.save(commit=False) is invoked and thus form.save_m2m() needs to be called.
Refs the doc:
Another side effect of using commit=False is seen when your model has
a many-to-many relation with another model. If your model has a
many-to-many relation and you specify commit=False when you save a
form, Django cannot immediately save the form data for the
many-to-many relation. This is because it isn't possible to save
many-to-many data for an instance until the instance exists in the
database.
To work around this problem, every time you save a form using
commit=False, Django adds a save_m2m() method to your ModelForm
subclass. After you've manually saved the instance produced by the
form, you can invoke save_m2m() to save the many-to-many form data.

Override save on Django InlineModelAdmin

That question might look similar to this one, but it's not...
I have a model structure like :
class Customer(models.Model):
....
class CustomerCompany(models.Model):
customer = models.ForeignKey(Customer)
type = models.SmallIntegerField(....)
I am using InlineModels, and have two types of CustomerCompany.type. So I define two different inlines for the CustomerCompany and override InlineModelAdmin.queryset
class CustomerAdmin(admin.ModelAdmin):
inlines=[CustomerCompanyType1Inline, CustomerCompanyType2Inline]
class CustomerCompanyType1Inline(admin.TabularInline):
model = CustomerCompany
def queryset(self, request):
return super(CustomerCompanyType1Inline, self).queryset(request).filter(type=1)
class CustomerCompanyType2Inline(admin.TabularInline):
model = CustomerCompany
def queryset(self, request):
return super(CustomerCompanyType2Inline, self).queryset(request).filter(type=2)
All is nice and good up to here, but for adding new records for InlineModelAdmin, I still need to display type field of CustomerCompany on the AdminForm, since I can not override save method of an InlineModelAdmin like:
class CustomerCompanyType2Inline(admin.TabularInline):
model = CustomerCompany
def queryset(self, request):
return super(CustomerCompanyType2Inline, self).queryset(request).filter(type=2)
#Following override do not work
def save_model(self, request, obj, form, change):
obj.type=2
obj.save()
Using a signal is also not a solution since my signal sender will be the same Model, so I can not detect which InlineModelAdmin send it and what the type must be...
Is there a way that will let me set type field before save?
Alasdair's answer isn't wrong, but it has a few sore points that could cause problems. First, by looping through the formset using form as the variable name, you actually override the value passed into the method for form. It's not a huge deal, but since you can do the save without commit right from the formset, it's better to do it that way. Second, the all important formset.save_m2m() was left out of the answer. The actual Django docs recommend the following:
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for instance in instances:
# Do something with `instance`
instance.save()
formset.save_m2m()
The problem you're going to run into is that the save_formset method must go on the parent ModelAdmin rather than the inlines, and from there, there's no way to know which inline is actually being utilized. If you have an obj with two "types" and all the fields are the same, then you should be using proxy models and you can actually override the save method of each to set the appropriate type automatically.
class CustomerCompanyType1(CustomerCompany):
class Meta:
proxy = True
def save(self, *args, **kwargs):
self.type = 1
super(CustomerCompanyType1, self).save(*args, **kwargs)
class CustomerCompanyType2(CustomerCompany):
class Meta:
proxy = True
def save(self, *args, **kwargs):
self.type = 2
super(CustomerCompanyType2, self).save(*args, **kwargs)
Then, you don't need to do anything special at all with your inlines. Just change your existing inline admin classes to use their appropriate proxy model, and everything will sort itself out.
There's a save_formset method which you could override. You'd have to work out which inline the formset represents somehow.
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for instance in instances:
# Do something with `instance`
instance.save()
formset.save_m2m()
Other answers are right when it comes to using save_formset. They are missing a way to check what model is currently saved though. To do that, you can just:
if formset.model == CustomerCompany:
# actions for specific model
Which would make the save_formset function look like: (assuming you just want to override save for the specific model(s))
def save_formset(self, request, form, formset, change):
for obj in formset.deleted_objects:
obj.delete()
# if it's not the model we want to change
# just call the default function
if formset.model != CustomerCompany:
return super(CustomerAdmin, self).save_formset(request, form, formset, change)
# if it is, do our custom stuff
instances = formset.save(commit=False)
for instance in instances:
instance.type = 2
instance.save()
formset.save_m2m()
For the cases where you need to take an action if the registry is new, you need to do it before saving the formset.
def save_formset(self, request, form, formset, change):
for form in formset:
model = type(form.instance)
if change and hasattr(model, "created_by"):
# craeted_by will not appear in the form dictionary because
# is read_only, but we can anyway set it directly at the yet-
# to-be-saved instance.
form.instance.created_by = request.user
super().save_formset(request, form, formset, change)
In this case I'm also applying it when the model contains a "created_by" field (because this is for a mixin that I'm using in many places, not for a specific model).
Just remove the and hasattr(model, "created_by") part if you don't need it.
Some other properties that might be interesting for you when messing with formsets:
"""
The interesting fields to play with are:
for form in formset:
print("Instance str representation:", form.instance)
print("Instance dict:", form.instance.__dict__)
print("Initial for ID field:", form["id"].initial)
print("Has changed:", form.has_changed())
form["id"].initial will be None if it's a new entry.
"""
I hope my digging helps someone else! ^^

Override of ModelAdmin.save_model not being called

I'm using the GenerickStackedInline which is a subclass of InlineModelAdmin which goes to ModelAdmin. When I override save_model method... it's not being called.
class LocatedItemStackedInline(generic.GenericStackedInline):
template = "admin/location_app/located_items/stacked.html"
model = LocatedItem
extra = 1
form = MyModelForm
raw_id_fields = ('location',)
def save_model(self, request, obj, form, change):
import ipdb;ipdb.set_trace()
super(LocatedItemStackedInline, self).save_model(request, obj, form, change)
def save_form(self, request, form, change):
import ipdb;ipdb.set_trace()
super(LocatedItemStackedInline, self).save_form(request, form, change)
So, I'm missing something?
Any clue?
Regards
The problem was that I was overriding the save_model method on the InlineAdmin instead of on the ModelAdmin itself.
Now is being called...
Cheers.
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model
describes the function you're talking about. My best guess is that you're confused about when and where that will be called. Also, are you sure you're actually working with the latest revision?
Edit: I'd guess that inline ModelAdmin objects may behave differently, given their otherwise special status.

How to access request.user from an admin ModelForm clean method?

I'm doing some stuff on 'clean' on an admin ModelForm:
class MyAdminForm(forms.ModelForm):
def clean(self):
# Some stuff happens...
request.user.message_set.create(message="Some stuff happened")
class MyAdmin(admin.ModelAdmin):
form = MyAdminForm
Other than the threadlocals hack - how do I access request.user to set a message? I can't pass it to the form constructor because doesn't get called from my code.
You can't do it on the form without passing the user into the form constructor. Instead you can use the ModelAdmin.save_model function which is given the request object.
The save_model method is given the
HttpRequest, a model instance, a
ModelForm instance and a boolean value
based on whether it is adding or
changing the object. Here you can do
any pre- or post-save operations.
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model
Edit:
Since you want to put the logic/messages in the clean function you could do something like:
class MyAdminForm(forms.ModelForm):
user_messages = []
def clean(self):
# Some stuff happens...
user_messages.append("Some stuff happened")
class MyAdmin(admin.ModelAdmin):
form = MyAdminForm
def save_model(self, request, obj, form, change):
for message in form.user_messages:
request.user.message_set.create(message=message)
Very late edit:
user.message_set is set to be deprecated in Django 1.4. You should instead use ModelAdmin.message_user. https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#django.contrib.admin.ModelAdmin.message_user
You would have to explicitly pass it there in constructor, which isn't a thing, that is usually done.
Are you sure you want to put that stuff into a form? What exactly you would like to do there? Isn't raising ValidationError enough?