0

I am trying to add a boolean field which is 'is_featured' column in a pivot table of Portfolio.

I want to add images gallery with multiple images but among them there will be a featured image or first one will be featured if there is more than one images is featured.

I have found that by 'through' is possible only if a reference table exist but that does not solve my problem when i want to a extra static column. 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)

Generally gallery will create a pivot table portfolio_portfolio_imageGallery where,
1. I want add another column 'is_feature' that could be nullable, or a boolean
2. and is it possible to have a checkbox, or text field for 'is_featured' column when i choose an image from admin

table detail,
id
portfolio_id
imageGallery_id
is_feature <- is extra column I want to add. Any help will be greatly appreciated.

Sharif
  • 87
  • 1
  • 13
  • For the `tag` or the `gallery`? The question text and code seem to contradict each other. – Willem Van Onsem Aug 07 '19 at 19:49
  • my mistake. Its corrected already. Its for gallery. Like I want to add another column 'is_featured' – Sharif Aug 07 '19 at 19:57
  • **1.** You could use [BooleanField](https://docs.djangoproject.com/en/2.2/ref/models/fields/#booleanfield) and set `null=True` like so `is_featured = models.BooleanField(null=True)`. This is dependent on the version of Django you are using. – Erion S Aug 07 '19 at 20:19
  • 1
    **2.** I think you are looking for something like [this](https://stackoverflow.com/questions/8227023/list-display-boolean-icons-for-methods) – Erion S Aug 07 '19 at 20:24
  • @EronV I am using latest version if Django. It will be better for me if you just explain with proper code and instruction. It will be greatly appreciated. – Sharif Aug 07 '19 at 20:26

0 Answers0