DEV
DEV

Reputation: 75

How to retrieve model fields in Django ModelForm?

I'm trying to retrieve 'category' and 'EN_name' fields as a ChoiceField into my ModelForm, but I'm not getting the choices that I put in my model, it's showing me empty select field with no choices.

What I want is to create a form that takes:

'category', 'EN_route' and 'EN_name' from Medicine model,


'notes' from Prescription model,


'dose', 'dose_unit', 'frequency' and 'duration' from MedicinePrescription model


These are my model.py classes:

This is Medicine model class:

class Medicine(models.Model):
    CATEGORY = (
        ('All', 'All'),
        ('Vitamins', 'Vitamins'),
        ('Analgesics', 'Analgesics'),
        ('Antacids', 'Antacids'),
        ('Antianxiety', 'Antianxiety'),
        ('Antifungals', 'Antifungals'),
        ('Antihypertensives', 'Antihypertensives'),
        ('Antipyretics', 'Antipyretics'),

    )
    category = models.CharField(max_length=100, null=True, choices=CATEGORY)
    stock = models.PositiveIntegerField(null=True)
    dose = models.FloatField(null=True)
    EN_name = models.CharField(max_length=100, null=True)
    EN_brand = models.CharField(max_length=100, null=True)
    EN_formula = models.CharField(max_length=100, null=True)
    EN_doseUnit = models.CharField(max_length=50, null=True)
    EN_route = models.CharField(max_length=50, null=True)
    AR_name = models.CharField(max_length=100, null=True)
    AR_brand = models.CharField(max_length=100, null=True)
    AR_formula = models.CharField(max_length=100, null=True)
    AR_doseUnit = models.CharField(max_length=50, null=True)
    AR_route = models.CharField(max_length=50, null=True)

    def __str__(self):
        return self.EN_name

Prescription model class:

class Prescription(models.Model):
    STATUS = (
        ('Active', 'Active'),
        ('Inactive', 'Inactive'),
    )
    visit = models.ForeignKey(Visit, null=True, on_delete=models.SET_NULL)
    status = models.CharField(max_length=100, choices=STATUS)
    expiration_date = models.DateTimeField(auto_now_add=True, null=True)
    notes = models.CharField(max_length=1000, null=True)

    def __str__(self):
        return self.status

This is MedicinePrescription model class:

class MedicinePrescription(models.Model):
    medicine = models.ForeignKey(Medicine, null=True, on_delete=SET_NULL)
    prescription = models.ForeignKey(
        Prescription, null=True, on_delete=SET_NULL)
    frequency = models.ForeignKey(Frequency, null=True, on_delete=SET_NULL)
    dose = models.FloatField(null=True)
    dose_unit = models.CharField(max_length=50, null=True)
    duration = models.CharField(max_length=50, null=True)

This is forms.py I made:

class PatientPrescriptionForm (ModelForm):
    patient = forms.CharField()
    category = forms.ChoiceField()
    EN_name = forms.ChoiceField()
    notes = forms.CharField(widget=forms.Textarea(attrs={'class': ''}))
    dose = forms.CharField()
    dose_unit = forms.CharField()
    EN_route = forms.CharField()

    class Meta:
        model = MedicinePrescription
        fields = ['patient', 'category', 'EN_name', 'dose',
                  'dose_unit', 'EN_route', 'frequency', 'duration', 'notes']

Upvotes: 0

Views: 177

Answers (1)

Ed Kohler
Ed Kohler

Reputation: 544

It looks like you need to define in your form's ChoiceField where to look for the choices, like this:

category = forms.ChoiceField(choices=Medicine.CATEGORY)

Upvotes: 1

Related Questions