[drf]model设置
Posted iiiiiher
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[drf]model设置相关的知识,希望对你有一定的参考价值。
//# 给model添加虚拟字段
class CeleryExampleResult(models.Model):
task_id = models.BigIntegerField(default=0)
task_instance_id = models.BigIntegerField()
create_user = models.CharField(max_length=128, verbose_name="创建者", help_text="创建者")
create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
def _task_instance_url(self):
return "http://192.168.2.101/?taskInstanceList&appId=%s#taskInstanceId=%s" % (self.task_id, self.task_instance_id)
task_instance_url = property(_task_instance_url)
class Meta: # 注意,是模型的子类,要缩进!
# db_table = 'celery_example_result'
ordering = ["task_id"]
verbose_name = "CeleryExampleResult执行结果表"
verbose_name_plural = verbose_name
# 注: 这个字段在drf里是不会被序列化,不生效
// get_FIELD_display
class Person(models.Model):
SHIRT_SIZES = (
('S', 'Small'),
('M', 'Medium'),
('L', 'Large'),
)
name = models.CharField(max_length=60)
shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)
>>> p = Person(name="Fred Flintstone", shirt_size="L")
>>> p.save()
>>> p.shirt_size
'L'
>>> p.get_shirt_size_display()
'Large'
# 注: 这个字段在drf里是不会被序列化,不生效
//增加一个字段属性(上面例子的本质)
class CeleryExampleResult(models.Model):
task_id = models.BigIntegerField(default=0)
task_instance_id = models.BigIntegerField()
create_user = models.CharField(max_length=128, verbose_name="创建者", help_text="创建者")
create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
@property
def get_task_id_display(self):
return "hello world, %s" % self.task_id
# 注: 这个字段在drf里是不会被序列化,不生效
//设计到数据是否存在, 数据逻辑处理, 可以将方法放到对应的models类中
class User(models.Model):
email = models.EmailField(max_length=32, null=False, default="", unique=True)
login_code = models.CharField(max_length=8, null=False, default="")
login_code_created_time = models.DateTimeField(null=True)
last_login_time = models.DateTimeField(null=True)
@classmethod
def create_login_code(cls, email):
'''用户信息记录,验证码生成,发送邮件'''
if email != "":
user = None
# 获取用户信息,若以前注册过则获取,否则创建用户信息
try:
user = User.objects.get(email=email)
except ObjectDoesNotExist as e:
user = User()
user.email = email
user.login_code = ""
# 生成验证码并发送邮件
if user.login_code == "" or user.login_code_created_time is None or timezone.now() - user.login_code_created_time > timedelta(minutes=5):
#生成验证码
user.login_code = ''.join(random.sample(string.digits + string.ascii_letters, 6))
user.login_code_created_time = timezone.now()
try:
#发送验证码给用户
send_mail(
'[webchat]登陆验证码',
'请使用验证码 {0} 进行登陆,有效时间5分钟'.format(user.login_code),
settings.EMAIL_FROM,
[email],
fail_silently=False,
)
except smtplib.SMTPException as e:
return False
else:
# 记录验证码到数据库
user.save()
return True
return True
return False
@classmethod
def login(cls, email, login_code):
'''登陆验证'''
try:
#获取用户
user = User.objects.get(email=email)
# 验证邮箱,验证码是否正确,同时验证验证码有效时间
if email != "" and login_code != "" and user.login_code_created_time and login_code.lower() == user.login_code.lower() and timezone.now() - user.login_code_created_time < timedelta(minutes=5):
# 更新数据库记录
user.login_code = ""
user.last_login_time = timezone.now()
user.save()
return user
return None
except ObjectDoesNotExist as e:
return None
def __str__(self):
return '{0}'.format(self.email)
//调用
code = 200 if User.create_login_code(email) else 400
以上是关于[drf]model设置的主要内容,如果未能解决你的问题,请参考以下文章