How to add pre-save processing when adding objects on the Django admin site

Development environment

models.py

models.py


from django.db import models


class Word(models.Model):
    """Word model"""

    word = models.CharField(max_length=15, unique=True)
    #Character field
    word_count = models.PositiveSmallIntegerField(blank=True, null=True)

    def __str__(self):
        return self.word

admin.py

admin.py


from django.contrib import admin

from .models import Word


class WordAdmin(admin.ModelAdmin):

    # save_Override model function
    def save_model(self, request, obj, form, change):
        word = obj.word  #Input value in word field

        # word_count
        word_count = len(word)

        obj.word_count = word_count
        obj.save()  #Save object


#Model and save_Specify model function
admin.site.register(Word, WordAdmin)

Complete!

Now, when you add or update an object on the management site, the specified pre-processing will be executed.

Recommended Posts

How to add pre-save processing when adding objects on the Django admin site
How to make only one data register on the Django admin screen
How to filter foreign keys that can be selected on the Django admin screen
How to add sudo when debugging
The story of failing to update "calendar.day_abbr" on the admin screen of django
How to fix the shit heavy when reading Google Cloud Storage images from Django deployed on GAE
Dynamically add fields to Form objects in Django
[Django] How to resolve errors when installing mysqlclient
How to print debug messages to the Django console
How to pass arguments when invoking python script from blender on the command line
Think about how to program Python on the iPad
[Python] [Django] How to use ChoiceField and how to add options
Python beginners try adding basic auth to Django admin
How to put Takoyaki Oishikunaru on the segment tree
How to get multiple model objects randomly in Django
How to disable the reference feature when outputting PyYaml
How to build a Django (python) environment on docker
How to use Django on Google App Engine / Python
How to enjoy Python on Android !! Programming on the go !!
How to run Django on IIS on a Windows server
[Django] How to redirect unlogged-in users to the login page
How to register the same data multiple times with one input on the Django management screen
How to delete "(base)" that appears in the terminal when Anaconda is installed on Mac
How to increase the processing speed of vertex position acquisition
How to write custom validations in the Django REST Framework
How to handle static files when deploying to production with Django
[Hyperledger Iroha] Notes on how to use the Python SDK
Don't lose to Ruby! How to run Python (Django) on Heroku
How to deploy the easiest python textbook pybot on Heroku
How to resolve CSRF Protection when using AngularJS with Django
How to generate a query using the IN operator in Django
I can't log in to the admin page with Django3
In Django, how to abbreviate the long displayed string as ....
Notes on how to use marshmallow in the schema library
Django --Overview the tutorial app on Qiita and add features (2)
How to print characters to the console before booting on ARM
How to return the data contained in django model in json format and map it on leaflet