首页 > 其他 > 详细

django model IntegerField字段约束

时间:2021-06-04 22:27:43      阅读:24      评论:0      收藏:0      [点我收藏+]
from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator


class JenkinsServices(models.Model): project_id = models.IntegerField(default=1, validators=[MaxValueValidator(1000), MinValueValidator(1)]) service_name = models.CharField(max_length=100, null=False) service_description = models.TextField(max_length=200, null=True) class Meta: db_table = "tbl_jenkins_services" verbose_name = ‘jenkins服务信息表‘ ordering = ["id"]

  

 

序列化器:

class JenkinsServiceSerializer(serializers.ModelSerializer):
    project_id = serializers.IntegerField(required=True, max_value=1000, min_value=1)
    service_name = serializers.CharField(max_length=100, required=True)
    service_description = serializers.CharField(max_length=200,
                                                allow_null=True, allow_blank=True)

    class Meta:
        model = models.JenkinsServices
        fields = "__all__"

 

 

如果是charfield:

用法类比上面

方案一:

class ProjectInformation(BaseModel):
    id = models.AutoField(primary_key=True)
    project_name = models.CharField(max_length=50, null=False, unique=True)
    tester_charge = models.CharField(max_length=100, null=False)
    product_manager = models.CharField(max_length=100, null=False)
    project_description = models.TextField(max_length=200, null=True)

  

方案二:

 

MinLengthValidator,MaxLengthValidator

django model IntegerField字段约束

原文:https://www.cnblogs.com/SunshineKimi/p/14850979.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!