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