# This is an auto-generated Django model module. # You'll have to do the following manually to clean this up: # * Rearrange models' order # * Make sure each model has one field with primary_key=True # * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior # * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table # Feel free to rename the models, but don't rename db_table values or field names. from django.db import models from django.utils import timezone class DemoUsers(models.Model): user_id = models.AutoField(primary_key=True) user_name = models.CharField(unique=True, max_length=255) first_name = models.CharField(max_length=255, blank=True, null=True) last_name = models.CharField(max_length=255, blank=True, null=True) email = models.CharField(unique=True, max_length=255, blank=True, null=True) hashed_password = models.CharField(max_length=255) enabled_flag = models.BooleanField() effective_from = models.DateTimeField() effective_to = models.DateTimeField(blank=True, null=True) created_by = models.CharField(max_length=255) creation_date = models.DateTimeField() last_updated_by = models.CharField(max_length=255, blank=True, null=True) last_update_date = models.DateTimeField(blank=True, null=True) class Meta: managed = False db_table = 'demo_users' def __str__(self): return self.user_name def save(self, *args, **kwargs): # 检查对象是否存在数据库中,即判断是否有主键pk if self.pk: # 对象已存在,表示是更新操作 # 在这里执行你希望在更新时发生的自动赋值逻辑 self.last_update_date = timezone.now() # 你的自定义更新逻辑 else: self.creation_date = timezone.now() super().save(*args, **kwargs) # 调用父类的save方法完成保存