Radio buttons in django admin

django radio button model
django admin many to-many
django admin custom form
django admin template
django admin dashboard
django admin permissions
django admin inline foreign key
django modelform widget

I am creating a quiz app in django. here is the model:

class Quiz(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    publish = models.BooleanField(default=False)

    def __unicode__(self):
        return self.title

class Question(models.Model):
    quiz = models.ForeignKey(Quiz)
    question = models.TextField()
    hint = models.TextField()

    def __unicode__(self):
        return self.question

class Option(models.Model):
    question = models.ForeignKey(Question)
    option = models.CharField(max_length=250)
    correct = models.BooleanField(default=False)

    def __unicode__(self):
        return self.title

Each question can only have one answer. This is where the problem comes in. I haven't been able to figure out how to write the admin form for Option model such that for each question, only one of the options can have correct=True.

I can use radio buttons for each question but don't know how to restrict them such that only one can be selected for one foreign key.

I'm not sure how that can be done with reverse look up.

I know this is not exactly what you want, but is quite close. Can you go ahead and try this in the admin.py

admin.site.register(Quiz)
#admin.site.register(Option) #Include this if required

class OptionInline(admin.TabularInline):
    model = Option

@admin.register(Question)
    class QuestionAdmin(admin.ModelAdmin):
    inlines = [
        OptionInline,
    ]

How to make radio buttons in Django admin, Django model with choices will make use of select by default. To use radio buttons, you will have to to use the RadioSelect widget. See django  In this article, we show how to create radio buttons in a Django form. Radio buttons are a group on buttons on a form that allows a user to make a choice. Only one radio button can be selected. Unlike check boxes, which can allow for multiple items to be selected, with radio buttons, only one item can be selected.

I understood your question like this: you want to change your code because only one option should be right. Why don't you add this line to Question:

right_answer = models.ForeignKey(Option)

Like this, you needn't the boolean correct. Your also could add a Manager to this who returns any option which should be chooseable (you override the method get_queryset(), and this manager should have this line at get_qeryset(): q.option_set.all() where q is there Question. (Without this any option, although its matched with another Question, could be marked.)

I'm not sure whether the secont part would work neither whether its a good way, but the first part is the important one.

Widgets | Django documentation, Radio buttons are a group on buttons on a form that allows a user to make a choice. Only one radio button can be selected. Unlike check boxes, which can allow  how can i create a radio button using Django model fields. #models.py GENDER_CHOICES = ( ('M', Male), ('F', 'Female') ) class Profile(models.Model): gender = models.CharField(choices=GENDER_CHOICES, max_length=128) the above field is rendering as a select field, But i want to make it as a radio button.

Use the RadioSelect widget with admin integration .

class SimpleAdmin:
    ...
    form SimpleForm
    fields = (..., 'my_option', ...)
    ...

Create the form with the extra options:

class SimpleForm(forms.ModelForm):
    class Meta:
        model = Simple
    my_option = ChoiceField(
        label = 'Options',
        choices = (
            (0, 'num 0'),
            (1, 'num 1'),
        ),
        initial = 0,
        widget = RadioSelect,
    )

You can see here a similar example with a chart into admin area

The Django admin site | Django documentation, python #django #models #radio #button #checbox #forms.Duration: 8:45 Posted: Sep 16, 2019 Is there a widget in Django 1.0.2 to render a models.BooleanField as two radio buttons instead of a checkbox?

class FooAdmin:
    form FooForm
    fields = ('gender', .....)

class FooForm(forms.ModelForm):
    class Meta:
        model = Foo
    gender = ChoiceField(
        label = 'Options',
        choices = (
            (0, 'Male'),
            (1, 'Female'),
        ),
        initial = 0,
        widget = RadioSelect,
    )

How to Create Radio Buttons in a Django Form, Furthermore, Django provides generic form editing views that can do almost all and delete authors using our own forms (rather than using the admin including text boxes, checkboxes, radio buttons, date pickers and so on. Django built in actions. Another downside of using Django actions is that the actions are not available in the detail view.To add buttons to the detail view you need to override the template — a

if you want to take choices for question and register model in admin panel look below code.

class Question(models.Model):
    quiz = models.ForeignKey(Quiz)
    question = models.TextField()
    hint = models.TextField()

    def __unicode__(self):
        return self.question

and in Option class add choices that you want to give

OPTIONS = (
     (choice1 : 'choice1'),
     (choice2 : 'choice2')
) 
class Option(models.Model):
    question = models.ForeignKey(Question)
    option = models.CharField(max_length=250, choices=OPTIONS)
    correct = models.BooleanField(default=False)

    def __unicode__(self):
        return self.title

After that perform migrations it will solve your problem.

For more information Click here

Python Django models add radio button and checkbox to forms , In that case Ill try and understand the documentation better. EDIT 1: from django import forms from .models import Order from django.forms import widgets class  The problem is that after clicking "Add another Related Model" more than once in the admin, the default value for the radio button (i.e. 'blue' checked) is not preserved. What's happening is that when the hidden inline form template is cloned the first time, it loses that default value.

Django Tutorial Part 9: Working with forms, books/admin.py from django.contrib import admin # Register your models here. from as 'drop-down' button, which can be changed to 'radio button' as below. If a field is present in radio_fields, Django will use a radio-button interface instead. Assuming group is a ForeignKey on the Person model: class PersonAdmin ( admin .

[Django]How do I add a radio button to my form in Django?, Examples Checkboxes and radio buttons. Consider Django's admin, where numerous items of data of several different types may need to be prepared for  Radio buttons Bootstrap radio button. A 'radio button' is a component used to allow a user to make a single choice among a number of options (whereas Checkboxes are used for selecting multiple options).

2. Admin page, Overview. An HTML Form is a group of one or more fields/widgets on a web page, which can be used to collect information from users for submission to a server. Forms are a flexible mechanism for collecting user input because there are suitable widgets for entering many different types of data, including text boxes, checkboxes, radio buttons, date pickers and so on.

Comments
  • Which version of Django are you using? Can we see your "admin.py" file? Also, your Option model's __unicode__ method isn't returning a valid value.
  • Use RadioSelect. It's like a normal drop down, it just gives you the option to save one value.