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)
Now, when you add or update an object on the management site, the specified pre-processing will be executed.
Recommended Posts