H
H
Hikmat Abdunabiev2015-03-18 10:44:06
Django
Hikmat Abdunabiev, 2015-03-18 10:44:06

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

There is a form:
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']

When a new object for Video is created in the form, html code is generated in the Genres field:
<label for="id_genres_0"><input id="id_genres_0" name="genres" value="11" type="checkbox"> Драма</label>
  ...

How to make sure that ID Category for the given Genres is added in these checkboxes?! For example, so?
<label for="id_genres_0" data-category-id="1"><input id="id_genres_0" name="genres" value="11" type="checkbox"> Драма</label>
  ...

Many thanks in advance for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
un1t, 2015-03-18
@un1t

You somehow localize your question, the question is not clear, and hardly anyone wants to look at so much code and try to understand the question. What is "parent ID for checkbox" is not clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question