AM0k
AM0k

Reputation: 90

Many to many fields in widget form

I have problem with display many to many field in form widget. Category is not display in template. Title is ok (is display) but category isn't - category is empty. What can I do to display many to many fields in my template form with multiplechoice checkboxes? Why I cant display article categories in widget form?

MODELS.py

article model:

class Article(Created, HitCountMixin):
    title = models.CharField(max_length=120)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    category = models.ManyToManyField(ArticleCategory, related_name='articles')

category model:

class ArticleCategory(Created):
    category_name = models.CharField(max_length=128)
    slug = models.SlugField(null=False, unique=False)

VIEWS:

class UpdateArticleView(LoginRequiredMixin, UpdateView):
    template_name = 'news/update_article.html'
    form_class = EditArticleForm
    model = Article

    def get_success_url(self):
        pk = self.kwargs["pk"]
        slug = self.kwargs['slug']
        return reverse_lazy("news:article_detail", kwargs={'pk': pk, 'slug': slug})

FORMS.py

class AddArticleForm(forms.ModelForm):

title = forms.CharField(
    label="Tytuł",
    max_length=120,
    help_text="Tytuł newsa",
    widget=forms.TextInput(attrs={"class": "form-control form-control-lg pr-5 shadow p-1 mb-1 bg-white rounded"}),
    required=True,
)


category = forms.MultipleChoiceField(
    widget=forms.CheckboxSelectMultiple,
)

And in my HTML TEMPLATE:

    <form method="post" enctype='multipart/form-data'>
        {% csrf_token %}
        {{ form.media }}
        {#        {%  crispy form %}#}
        {{ form|crispy }}
        <button type="submit" class="btn btn-outline-primary">EDYTUJ NEWS</button>
    </form>

Upvotes: 1

Views: 985

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

Your form_class in your view is a EditArticleForm, so you should be careful to use the correct form.

The form field for a ManyToManyField is normally a ModelMultipleChoiceField [Django-doc], but it is not necessary to specify the form field anyway. You can make use of the widgets option:

class EditArticleForm(forms.ModelForm):
    title = forms.CharField(
        label='Tytuł',
        max_length=120,
        help_text='Tytuł newsa',
        widget=forms.TextInput(
            attrs={'class': 'form-control form-control-lg pr-5 shadow p-1 mb-1 bg-white rounded'}
        ),
        required=True,
    )


    class Meta:
        model = Article
        widgets = {
            'category': forms.CheckboxSelectMultiple
        }

you can customize the label with:

class EditArticleForm(forms.ModelForm):
    title = forms.CharField(
        label='Tytuł',
        max_length=120,
        help_text='Tytuł newsa',
        widget=forms.TextInput(
            attrs={'class': 'form-control form-control-lg pr-5 shadow p-1 mb-1 bg-white rounded'}
        ),
        required=True,
    )


    class Meta:
        model = Article
        widgets = {
            'category': forms.CheckboxSelectMultiple
        }
        labels = {
            'category': 'label of category'
        }

Upvotes: 2

Related Questions