noob87
noob87

Reputation: 41

Passing a callable to the default option of a model field fails

I have a model named Package. It has fields named, diagnosis, treatment, patient_type, mf and tp. The fields diagnosis, treatment and patient_type have foreign keys defined in separate individual classes, making diagnosis, treatment and patient_type choice fields. Now, what I intend to achieve is that whenever the user chooses the treatment and patient_type from the choices, I want certain figure to appear in mf as a default value. It should be dependant on both the above fields. I have defined a function that takes treatment and patient_type as its arguments, applies the suitable conditions and returns the relevant figure. But when I assign the function to the default option to the mf field, run the migrations and open the webpage and fill in the details in treatment and patient_type, the mf does not return the desired value. What is going on here, is there something that I am doing wrongly?

The models:

class Diagnosis(models.Model):
    diagnosis=models.CharField(max_length=30, blank=True)

    def __str__(self):
        return self.diagnosis

class Treatment(models.Model):
    treatment=models.CharField(max_length=15, blank=True)

    def __str__(self):
        return self.treatment

class PatientType(models.Model):
    patient_type=models.CharField(max_length=15, blank=True)

    def __str__(self):
        return self.patient_type


class Package(models.Model):
    rt_number=ForeignKey(Patient, on_delete=CASCADE)
    diagnosis=models.ForeignKey(Diagnosis, on_delete=CASCADE)
    treatment=ForeignKey(Treatment, on_delete=CASCADE)
    patient_type=ForeignKey(PatientType, on_delete=CASCADE)
    max_fractions=models.IntegerField(default=max_fraction(treatment, patient_type))
    total_package=models.DecimalField(max_digits=10, decimal_places=2)

The function that is added to the default option in the mf field:

def max_fraction(tt, ptt):
    
    if tt=='YEO5' and ptt=='RUHPE':
        return 40
    if tt=='D4EG' and ptt=='KILEU':
        return 40
    if tt=='5GED' and ptt=='IMRA':
        return 40

The forms:

class DiagnosisForm(ModelForm):
    class Meta:
        model=Diagnosis
        fields='__all__'

class TreatmentForm(ModelForm):
    class Meta:
        model=Treatment
        fields='__all__'

class PatientTypeForm(ModelForm):
    class Meta:
        model=PatientType
        fields='__all__'

class PackageForm(ModelForm):
    class Meta:
        model=Package
        fields='__all__'

The views.py:

def package_view(request):
    if request.method=='POST':
        fm_package=PackageForm(request.POST)
        fm_diagnosis=DiagnosisForm(request.POST)
        fm_treatment=TreatmentForm(request.POST)
        fm_patient_type=PatientTypeForm(request.POST)
        if fm_package.is_valid() and fm_diagnosis.is_valid() and fm_treatment.is_valid() and fm_patient_type.is_valid():
            diagnosis=fm_diagnosis.save()
            treatment=fm_treatment.save()
            patient_type=fm_patient_type.save()
            package=fm_package.save(False)
            package.diagnosis=diagnosis
            package.treatment=treatment
            package.patient_type=patient_type
            package.save()
            fm_package=PackageForm()
            fm_diagnosis=DiagnosisForm()
            fm_treatment=TreatmentForm()
            fm_patient_type=PatientTypeForm()
        return render (request, 'account/package.html', {'form2':fm_diagnosis, 'form3':fm_treatment, 'form4':fm_patient_type, 'form5':fm_package})
    else:
        fm_package=PackageForm()
        fm_diagnosis=DiagnosisForm()
        fm_treatment=TreatmentForm()
        fm_patient_type=PatientTypeForm()
        return render (request, 'account/package.html', {'form2':fm_diagnosis, 'form3':fm_treatment, 'form4':fm_patient_type, 'form5':fm_package})

The template:

<form action="" method="post" novalidate>
        {% csrf_token %}
        {{form2.as_p}}
        {{form3.as_p}}
        {{form4.as_p}}
        {{form5.as_p}}
        <input type="submit" value="Submit">
    </form>

The URLs:

path('package/', views.package_view),

Please, help me out here.

Upvotes: 1

Views: 163

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477686

The default=… can work with a callable, but that callable does not have any parameters.

What might be a more elegant solution is make the field NULL-able and then define a property that will in case of a None determine the value, so something like:

class Package(models.Model):
    # …
    _max_fractions=models.IntegerField(default=None, null=True)
    # …
    
    @property
    def max_fractions(self):
        if self._max_fractions is None:
            return max_fraction(self.treatment, self.patient_type)
        return self._max_fractions

    @max_fractions.setter
    def _set_max_fractions(self, value):
        self._max_fractions = value

Upvotes: 2

Related Questions