2

I would like to set up a radio select option in admin for my blog's category. ManyToMany fields do not work with a RadioSelect widget.

I want the category to be a ManyToOne relationship with the articles. Right now I have a ParentalManyToMany field and I register the snippet for the blog category.

class BlogPage(Page):
    ...
    category = ParentalManyToManyField('blog.ArticleCategory', blank=True)
    ...


@register_snippet
class ArticleCategory(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField(unique=True, max_length=80)

    panels = [
        FieldPanel('name'),
        FieldPanel('slug'),
    ]

    def __str__(self):
        return self.name

I don't know how to change this into a ManyToOne option, so I could have a radioselect instead of a CheckboxSelectMultiple.

Help would be appreciated. Thanks!

AJH
  • 265
  • 2
  • 12

1 Answers1

2

A many-to-one relationship is a ForeignKey field. These will use a select dropdown as the form field by default, but you can override this by passing a widget argument on the FieldPanel:

from django import forms

class BlogPage(Page):
    ...
    category = models.ForeignKey('blog.ArticleCategory', null=True, blank=True, on_delete=models.SET_NULL)

    content_panels = [
       ...
       FieldPanel('category', widget=forms.RadioSelect),
    ]
gasman
  • 23,691
  • 1
  • 38
  • 56