python - 如何向 ManyToManyField Django 添加可为空或布尔字段?
问题描述
我正在尝试在投资组合的数据透视表中添加一个布尔字段,它是“is_featured”列。
我想添加包含多张图片的图片库,但其中会有一张特色图片,或者如果有多个图片被推荐,则将展示第一个。
我发现只有在存在引用表的情况下才能通过“通过”,但是当我想要一个额外的静态列时,这并不能解决我的问题。
http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships
from django.db import models
from tinymce.models import HTMLField
from category.models import Category
from imageGallery.models import ImageGallery
from tag.models import Tag
class Portfolio(models.Model):
title = models.CharField(max_length=191)
description = HTMLField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
tag = models.ManyToManyField(Tag, blank=True)
gallery = models.ManyToManyField(ImageGallery, blank=True) # > Here I want to add another column 'is_feature' !!!!
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
一般来说,画廊将创建一个数据透视表portfolio_portfolio_imageGallery,其中,
1.我想添加另一个可以为空的列'is_feature'或布尔值
2.当我选择时,是否可以有一个复选框或'is_featured'列的文本字段来自管理员的图像
表详细信息,
id
组合
_id imageGallery_id
is_feature <-是我要添加的额外列。任何帮助将不胜感激。
解决方案
推荐阅读
- javascript - 如何将 AVA 和 Cypress 测试链接在一起以进行完整的端到端测试?
- javascript - 如何使用 redux store 正确更新 Draft.js 中的 EditorState
- sql - 如果记录不存在,则 SQL 插入无论如何都会插入
- c++ - QSqlQueryModel 使用和性能
- javascript - 如何在 Node.js 中调用 $.ajax
- node.js - 我制作了一个连接到 mlab 上的 mongo 数据库的 nodejs express 服务器。如何从我的 React 应用程序将 CRUD 功能连接到此服务器?
- javascript - 按空格分割字符串,忽略嵌套字符串中的空格
- javascript - 传真 API 的 Node.js Twilio HTTP 检索失败
- java - Apache Ant:执行后续目标而不等待完成
- node.js - 来自守护进程的 Docker 错误响应:OCI 运行时创建失败