转自:https://www.jianshu.com/p/a86281df530e
Django多条件筛选查询
主模型只存在外键一对多关系
模型设计
- # 快捷筛选状态
- class Status(models.Model): order_number = models.PositiveIntegerField(unique=True, verbose_name='状态编号') status_tag = models.CharField(max_length=10, verbose_name='状态名称') class Meta: ordering = ['order_number', ] verbose_name = '事件选择' verbose_name_plural = verbose_name def __str__(self): return self.status_tag # 项目分类 class Project(models.Model): project_name = models.CharField(max_length=10, verbose_name='项目名称') class Meta: ordering = ['project_name'] verbose_name = '项目分类' verbose_name_plural = verbose_name def __str__(self): return self.project_name # 事件分类 class Category(models.Model): category_name = models.CharField(max_length=10, verbose_name='分类名称') class Meta: ordering = ['category_name', ] verbose_name = '事件分类' verbose_name_plural = verbose_name def __str__(self): return self.category_name # 事件级别 class Level(models.Model): order_number = models.PositiveIntegerField(unique=True, verbose_name='级别编号') level_tag = models.CharField(max_length=10, verbose_name='级别名称') class Meta: ordering = ['order_number', ] verbose_name = '事件级别' verbose_name_plural = verbose_name def __str__(self): return self.level_tag # 事件内容 class EventContent(models.Model): title = models.CharField(max_length=50, verbose_name='事件标题') content = models.TextField(verbose_name='事件正文') image = models.ImageField(upload_to='images/%Y/%m', blank=True, null=True, verbose_name='描述图片') created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间') updated = models.DateTimeField(auto_now=True, verbose_name='更新时间') status = models.ForeignKey(Status, on_delete=models.SET_NULL, null=True, blank=True, related_name='event_content', verbose_name='事件状态') project = models.ForeignKey(Project, on_delete=models.SET_NULL, null=True, blank=True, related_name='event_content'