标签:osi mode lse sel djang meta length eal return
1,在save时,首先可以做如下操作
from django.db import models class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def save(self, *args, **kwargs): do_something() #在之前做操作 super(Blog, self).save(*args, **kwargs) # Call the "real" save() method. do_something_else() #在之后做操作
from django.db import models
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def save(self, *args, **kwargs): #在保存中做判断
if self.name == "Yoko Ono‘s blog":
return # Yoko shall never have her own blog!
else:
super(Blog, self).save(*args, **kwargs) # Call the "real" save() method.
2,抽象基类
当您想要将一些公共信息放入许多其他模型时,抽象基类非常有用。你写你的基类,并把abstract=True在元 类。然后,
此模型将不用于创建任何数据库表。相反,当它用作其他模型的基类时,其字段将添加到子类的字段中。
from django.db import models
class CommonInfo(models.Model):
name = models.CharField(max_length=100)
age = models.PositiveIntegerField()
class Meta:
abstract = True
class Student(CommonInfo):
home_group = models.CharField(max_length=5)
3
标签:osi mode lse sel djang meta length eal return
原文地址:https://www.cnblogs.com/52forjie/p/9403544.html