The Django framework comes with a powerful administrative tool called admin. You can use it out of the box to quickly add, delete, or edit any database model from a web interface. But with a little extra code, you can customize the Django admin to take your admin capabilities to the next level.
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.
The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.
default: The default value for the field. This can be a value or a callable object, in which case the object will be called every time a new record is created. null: If True , Django will store blank values as NULL in the database for fields where this is appropriate (a CharField will instead store an empty string).
From the Django Docs:
ModelAdmin.save_model(self, request, obj, form, change)
The
save_model
method is given theHttpRequest
, a model instance, aModelForm
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.For example to attach
request.user
to the object prior to saving:
from django.contrib import admin
class ArticleAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.user = request.user
super().save_model(request, obj, form, change)
class ArticleAdmin( admin.ModelAdmin ):
fields = [ 'title', 'body' ]
form = ArticleAdminForm
def save_model(self, request, obj, form, change):
obj.author = "name Here"
obj.save()
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