Answer the question
In order to leave comments, you need to log in
How to add a parent ID to a checkbox in Django?
Good afternoon People!
Please help, how can I display the parent ID for the checkbox! I searched in the Internet and did not find anything :(
There is a model:
class VideoCategory(models.Model):
"""
Категория видеоматериала (Пример: Художественный, Документальный, Музыкальный и т.д.)
"""
name = models.CharField('категория', max_length=50, unique=True)
class Meta:
permissions = (
('view_videocategory', 'Can see available VideoCategory'),
)
verbose_name = 'категория'
verbose_name_plural = 'категории'
def __str__(self):
return self.name
class VideoGenre(models.Model):
"""
Жанр видео в связке с категорией (Пример: Для Кино - Драма, Комедия, Боевик и т.д.)
"""
category = models.ForeignKey(VideoCategory, verbose_name='категория', related_name='category')
name = models.CharField('жанр', max_length=100, unique=True)
class Meta:
permissions = (
('view_videogenre', 'Can see available VideoGenre'),
)
ordering = ['name']
verbose_name = 'жанр'
verbose_name_plural = 'жанры'
def __str__(self):
return self.name
class VideoForm(forms.ModelForm):
"""
Форма видеоматериала
"""
class Meta:
model = Video
fields = ['fund', 'title', 'title_alt', 'index_type', 'mediatype', 'timekeeping', 'category', 'genres', 'country', 'language', 'release_date', 'release_month', 'release_year', 'manufacturer', 'color', 'season', 'episode', 'director', 'screenwriter', 'cameraman', 'composer', 'artist', 'cast', 'short_content', 'age_limit', 'rating', 'status']
widgets = {
'color': forms.RadioSelect,
'genres': forms.CheckboxSelectMultiple,
'status': forms.RadioSelect,
}
def __init__(self, *args, **kwargs):
super(VideoForm, self).__init__(*args, **kwargs)
self.fields['genres'].help_text = ''
self.fields['genres'].queryset = VideoGenre.objects.all().order_by('category__name')
for key, field in self.fields.items():
if isinstance(field.widget, forms.TextInput):
field.widget.attrs.update({'placeholder': field.label,})
class VideoCategoryForm(forms.ModelForm):
"""
Форма категории
"""
class Meta:
model = VideoCategory
fields = ['name']
class VideoGenreForm(forms.ModelForm):
"""
Форма жанра
"""
class Meta:
model = VideoGenre
fields = ['category', 'name']
<label for="id_genres_0"><input id="id_genres_0" name="genres" value="11" type="checkbox"> Драма</label>
...
<label for="id_genres_0" data-category-id="1"><input id="id_genres_0" name="genres" value="11" type="checkbox"> Драма</label>
...
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question