Сделал через ManyToManyField
Создал таблицу с регионами:
class Region(models.Model):
region_name = models.CharField(verbose_name='Название', max_length=100)
region_id = models.PositiveIntegerField(verbose_name='Код субъекта', default=0)
def __str__(self):
return f'{self.region_id:02} - {self.region_name}'
class Meta:
verbose_name_plural = 'Регионы'
verbose_name = 'регион'
Добавил в модель models.ManyToManyField(Region, blank=True)
class Geography(SingletonModel):
title = models.CharField(verbose_name='Заголовок', max_length=200)
slug = models.SlugField(verbose_name='Алиас', unique=True, blank=True, max_length=255)
image = models.ImageField(verbose_name='Картинка', blank=True, upload_to='images')
text = models.TextField(verbose_name='Текст')
regions = models.ManyToManyField(Region, blank=True)
def save(self):
super(Geography, self).save()
if not self.slug:
self.slug = slugify(self.title) + '-' + str(self.id)
super(Geography, self).save()
def __str__(self):
return self.title
class Meta:
verbose_name_plural = 'География'
verbose_name = 'география'
А во view добавил:
from django.db import models
from django.forms import CheckboxSelectMultiple
class GeographyAdmin(admin.ModelAdmin):
formfield_overrides = {
models.ManyToManyField: {'widget': CheckboxSelectMultiple},
}