Skip to content

Rendering text choices in a UnicornView #324

Open
@WVandergrift

Description

@WVandergrift

I have a UnicornView that I'm using to display a form with multiple fields that have TextChoices. What's the best way to do this with Unicorn? In a standard django view, I'd use the ModelForm to handle rendering the form field, but I'm not sure that approach would work without requiring reloading the page.

NewCourseView:

from django_unicorn.components import UnicornView
from curriculum.forms import NewCourseForm


class NewCourseView(UnicornView):
    form_class = NewCourseForm

    title = ""
    grade_level = ""
    subject = "" 

NewCourseForm:

from django.forms import ModelForm
from .models import Course


class NewCourseForm(ModelForm):
    class Meta:
        model = Course
        fields = ['title', 'grade_level', 'subject']

Course Model:

class Course(models.Model):

    class GradeLevel(models.TextChoices):
        UNDEFINED = 'U', "UNDEFINED"
        KINDERGARTEN = 'K', "KINDERGARTEN"
        FIRST_GRADE = '1st', "FIRST_GRADE"
        SECOND_GRADE = '2nd', "SECOND_GRADE"
        NINTH_GRADE = '9th', "NINTH_GRADE"

    class Subject(models.TextChoices):
        UNDEFINED = "Undefined", "UNDEFINED",
        MATH = "Math", "MATH",
        SCIENCE = "Science", "SCIENCE"

    title = models.CharField(max_length=256)
    grade_level = models.CharField(
        max_length=10,
        choices=GradeLevel.choices,
        default=GradeLevel.UNDEFINED
    )
    subject = models.CharField(
        max_length=25,
        choices=Subject.choices,
        default=Subject.UNDEFINED
    )

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions