一、新建一个django项目,注册两个app分别为 tournament、comment,其中models的代码分别为:
# tournament/models.py from django.db import models class Club(models.Model): region_choices = [ ('E', 'East'), ('W', 'West'), ] name = models.CharField(max_length=50) region = models.CharField(max_length=20, choices=region_choices) desc = models.TextField(max_length=1000, null=True, blank=True) class Meta: verbose_name = "俱乐部" verbose_name_plural =verbose_name def __str__(self): return self.name class Player(models.Model): name = models.CharField(max_length=50) age = models.IntegerField() club = models.ForeignKey(Club, on_delete=models.CASCADE, null=True) salary = models.CharField(max_length=30) desc = models.TextField(max_length=1000) class Meta: verbose_name = "球员" verbose_name_plural = verbose_name def __str__(self): return self.name class News(models.Model): title = models.CharField(max_length=150) content = models.TextField(max_length=2000) players = models.ManyToManyField(Player) create_time = models.DateTimeField() comment = models.ForeignKey('comment.Comment', on_delete=models.CASCADE) class Meta: verbose_name = "新闻" verbose_name_plural = verbose_name def __str__(self): return self.title
# comment/models.py from django.db import models class Comment(models.Model): content = models.CharField(max_length=200) create_time = models.DateTimeField() sub_content = models.ForeignKey('self', on_delete=models.CASCADE) class Meta: verbose_name = "评论" verbose_name_plural = verbose_name def __str__(self): return self.content
二、filed options 即字段的一些参数设置
1. null = True,默认的null为False。这是针对数据库层面而言的,上面的club表:
可以看到,前面几列,创建model实例的时候,没有传desc字段时候,表中会默认存个NULL。
2. blank=True,一般表示前端输入时是否可以为空,默认为False。还是以上面的club的创建实例为例,可以看到,除了desc外,其他的的必须为空:
3. db_column 可以指定生成表的列名
4. help_text 可以指定表单中的辅助性说明文字
标签:verbose,models,max,self,django,入门篇,length,name From: https://www.cnblogs.com/m-chen/p/16993975.html