Well, I want to save any instance of a model without taking care about DDBB structure. So I decide to override def save
in every model´s class. Kind of:
def save(self, force_insert=False, force_update=False, using=None, update_fields=None): if condition: raise Exception("You can´t insert that element") return super(name_class, self).save(self, force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields)
Well, with this I achieve to insert not raising an exception, but if the instance passes this check I want to insert in the DB whatever primary restriction exists...
How can I get it?
I suppose I must have to override the core code of save
, but I checked it and I didn't find the part where I check the conditions for inserting in the DB. Maybe, The problem is only in the validation of the form.
How can I override a specific form in Django Admin? Specifically, that one where I add, Delete or Edit one class of the model.
save() method from its parent class is to be overridden so we use super keyword. slugify is a function that converts any string into a slug. so we are converting the title to form a slug basically.
The simplest option is to set save_as=True on the ModelAdmin . This will replace the "Save and add another" button with a "Save as new" button.
save() , django will save the current object state to record. So if some changes happens between get() and save() by some other process, then those changes will be lost.
You can overwrite save_model
of ModelAdmin
.
class MyAdminView(admin.ModelAdmin): def save_model(self, request, obj, form, change): super(MyAdminView, self).save_model(request, obj, form, change)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With