How do I flag only one of the formsets in django admin?

Posted by azuer88 on Stack Overflow See other posts from Stack Overflow or by azuer88
Published on 2010-03-29T10:49:15Z Indexed on 2010/03/29 10:53 UTC
Read the original article Hit count: 353

I have these (simplified) models:

class Question(models.Model):
    question = models.CharField(max_length=60)
class Choices(models.Model):
    question = models.ForeignKey(Question)
    text = models.CharField(max_length=60)
    is_correct = models.BooleanField(default=False)

I've made Choices as an inline of Question (in admin). Is there a way to make sure that only one Choice will have is_correct = True?

Ideally, is_correct will be displayed as a radio button when it is displayed in the admin formset (TabularInline).

my admin.py has:

from django.contrib import admin

class OptionInline(admin.TabularInline):
    model = Option
    extra = 5
    max_num = 5

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

admin.site.register(QType)
admin.site.register(Question, QuestionAdmin)

© Stack Overflow or respective owner

How do I flag only one of the formsets in django admin ?

Posted by azuer88 on Stack Overflow See other posts from Stack Overflow or by azuer88
Published on 2010-03-29T08:59:44Z Indexed on 2010/03/29 9:03 UTC
Read the original article Hit count: 353

I have these (simplified) models:
class Question(models.Model): question = models.CharField(max_length=60) class Choices(models.Model): question = models.ForeignKey(Question) text = models.CharField(max_length=60) is_correct = models.BooleanField(default=False)

I've made Choices as an inline of Question (in admin). Is there a way to make sure that only one Choice will have is_correct = True?

Ideally, is_correct will be displayed as a radio button when it is displayed in the admin formset (TabularInline).

My first solution was to override the validation of the formset but did understand how to do this. (is_correct is displayed as checkbox, and I'd display an error that only one is_correct should be selected.)

© Stack Overflow or respective owner

Related posts about django-admin

Related posts about django