首页 > 解决方案 > 如何向 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 <-是我要添加的额外列。任何帮助将不胜感激。

标签: pythondjangodjango-admin

解决方案


推荐阅读