Home > Enterprise >  Cannot assign "'75f37671bac8'": "Results.patient" must be a "Pati
Cannot assign "'75f37671bac8'": "Results.patient" must be a "Pati

Time:10-16

Patient

I'm trying to add a patient to my result form, but it says Cannot assign "'75f37671bac8'": "Results.patient" must be a "Patient" instance. I tried everything but it seems not change, I'll really appreciate some help. Thanks for your help in advance.

Models.py

class Patient(models.Model):
    uniqueId = models.CharField(null=True, blank=True, max_length=100)
    slug = models.SlugField(max_length=500, unique=True, blank=True, null=True)
    date_created = models.DateTimeField(blank=True, null=True)
    last_updated = models.DateTimeField(blank=True, null=True)


class Results(models.Model):
    title = models.CharField(null=True, blank=True, max_length=100)
    number = models.CharField(null=True, blank=True, max_length=100)
    dueDate = models.DateField(null=True, blank=True)
    status = models.CharField(choices=STATUS, default='CURRENT', max_length=100)
    notes = models.TextField(null=True, blank=True)

    #RELATED fields
    patient = models.ForeignKey(Patient, blank=True, null=True, on_delete=models.SET_NULL)

views.py

if request.method == 'POST':
        doc_form  = DoctorForm(request.POST)
        inv_form = ResultsForm(request.POST, instance=results)
        patients_form = PatientSelectForm(request.POST, initial_patient=results.patient, instance=results)

        if doc_form.is_valid():
            obj = doc_form.save(commit=False)
            obj.results = results
            obj.save()

            messages.success(request, "Results Doctor added succesfully")
            return redirect('create-build-results', slug=slug)
        elif inv_form.is_valid and 'paymentTerms' in request.POST:
            inv_form.save()

            messages.success(request, "Results updated succesfully")
            return redirect('create-build-results', slug=slug)
        elif patients_form.is_valid() and 'patient' in request.POST:

            patients_form.save()
            messages.success(request, "Client added to Results succesfully")
            return redirect('create-build-results', slug=slug)

forms.py

class PatientSelectForm(forms.ModelForm):

    def __init__(self,*args,**kwargs):
        self.initial_patient = kwargs.pop('initial_patient')
        self.PATIENT_LIST = Prescription.objects.all()
        self.PATIENT_CHOICES = [('-----', '--Select a Patient--')]
        for patient in self.PATIENT_LIST:
            d_t = (patient.uniqueId, patient.first_name)
            self.PATIENT_CHOICES.append(d_t)
        super(PatientSelectForm,self).__init__(*args,**kwargs)

        self.fields['patient'] = forms.ChoiceField(
                                        label='Choose a related patient',
                                        choices = self.PATIENT_CHOICES,
                                        widget=forms.Select(attrs={'class': 'form-control mb-3'}),)

    class Meta:
        model = Results
        fields = ['patient']

traceback line

elif patients_form.is_valid() and 'patient' in request.POST:

CodePudding user response:

Patient is a related field to Results, when you build your form you are selecting from a drop-down the following: d_t = (patient.uniqueId, patient.first_name)

This is selecting the uniqueId of the patient, not an actual Patient instance. You need an extra step to create the Patient instance from the uniqueId. So add this code to your form:

    def clean_patient(self):
        c_patient = self.cleaned_data['patient']
        if c_patient == '':
            return None
        else:
            return Patient.objects.get(uniqueId=c_patient)
    

You need to add it after the class meta, like so:

class Meta:
    model = Results
    fields = ['patient']

def clean_patient(self):
        c_patient = self.cleaned_data['patient']
        if c_patient == '':
            return None
        else:
            return Patient.objects.get(uniqueId=c_patient)
  • Related