Ahmed Ibrahim
Ahmed Ibrahim

Reputation: 517

How to select muliple choices from another model?

Lab_Group Model

class Lab_Group(models.Model):
    group = models.CharField(max_length=100, unique=True,)

Lab Model

class Lab(models.Model):
    laboratory = models.CharField(max_length=50, unique=True)
    group = models.ForeignKey(Lab_Group, on_delete=models.CASCADE)

Lab Request Model

class LabRequest(models.Model):
    ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
    lab_test = models.ManyToManyField(Lab)

As you see I have these models each one relates the other one. In the LabRequest model I want to select some list from Lab model as multiple choice .

I have a template where I want to select a lab_test from Lab model as checkboxes. Please guide me on how do I do that.

Upvotes: 1

Views: 38

Answers (1)

bmons
bmons

Reputation: 3392

you can use django widgets, link:https://docs.djangoproject.com/en/2.2/ref/forms/widgets/#checkboxselectmultiple

lab_test = forms.MultipleChoiceField(
        required=False,
        widget=forms.CheckboxSelectMultiple,
        queryset=Lab.objects.all()
    )

Upvotes: 1

Related Questions