Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update field on another table when submitting form – django

I have a form where adding appointments. I would like to update a field in another table during submission. Let me illustrate:

accounts model (Custom user model)

class Account(AbstractBaseUser):

    patient_status = (     
        ('No Patient', _('No Patient')),
        ('New Patient', _('New Patient')),
        ('Patient', _('Patient')),
    )

    first_name = models.CharField(_('First Name'), max_length=50)
    last_name = models.CharField(_('Last Name'), max_length=50)
    username = models.CharField(_('Username'), max_length=50, unique=True)
    ...
    # required
    is_patient = models.CharField(_('Patient'), max_length=20, choices=patient_status)
    ...

views.py adding appointment:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def add_appointment(request):
    form = AddAppointmentForm(request.POST or None)
    
    if request.method == 'POST':
        if form.is_valid():
            appoint_user = form.cleaned_data.get('user')
            appoint_seat = form.cleaned_data.get('seat')
            appoint_start_appointment = form.cleaned_data.get('start_appointment')
            appoint_end_appointment = form.cleaned_data.get('end_appointment')
            
            # If Appointment already exist 
            if Appointment.objects.filter(user=appoint_user, seat=appoint_seat, start_appointment=appoint_start_appointment, end_appointment=appoint_end_appointment).exists():
                messages.warning(request, "This appointment already exists.")

            else:
                form.save()

                messages.success(request, 'Appointment was added successfully!')
                return redirect('appointments:add_appointment')
    else:
        form = AddAppointmentForm()

I would like to update is_patient after is_valid(), something like:

patient = Appointment.objects.filter(user=appoint_user).count()
if patient > 0:
  patient.user__is_patient = 'Patient'
  patient.save()

How can i access is_patient from Account’s table in order to update and also where is the right place to put the code, in the view?

Update with appointments model

class Appointment(models.Model):
    
    APPOINTMENT_STATUS = (
        ('New', _('New')),
        ('Finished', _('Finished')),
        ('Rescheduled', _('Rescheduled')),
        ('Cancelled', _('Cancelled')),
        ('Notshow', _('Notshown')),
    )

    DURATION = (
        ('Notstarted', _('Not Started')),
        ('Checkin', _('Check In')),
        ('TransferToSeat', _('Transfer to Seat')),
        ('Completed', _('Completed')),
    )

    user = models.ForeignKey(Account, on_delete=models.CASCADE)
    seat = models.ForeignKey(Seat, on_delete=models.SET_NULL, null=True)
    start_appointment = models.DateTimeField(default=timezone.now, blank=True)
    end_appointment = models.DateTimeField(default=timezone.now, blank=True)
    name = models.CharField(max_length=255)
    appointment_notes = models.TextField(_('Appointment Notes'), max_length=1500, null=True, blank=True)
    status = models.CharField(_('Appointment Status'), max_length=20, choices=APPOINTMENT_STATUS, default='New')
    duration = models.CharField(_('Duration'), max_length=20, choices=DURATION, default='Not Started')
    confirmed = models.BooleanField(_('Confirmed'), default=False)
    emailed = models.BooleanField(_('Emailed'), default=False)
    date_created = models.DateTimeField(_('Date Created'), auto_now_add=True)
    date_updated = models.DateTimeField(_('Date Updated'), auto_now=True)

Update after adding code:

if request.method == 'POST':
        if form.is_valid():
            appoint_user = form.cleaned_data.get('user')
            appoint_seat = form.cleaned_data.get('seat')
            appoint_start_appointment = form.cleaned_data.get('start_appointment')
            appoint_end_appointment = form.cleaned_data.get('end_appointment')
            
            # If Appointment already exist 
            if Appointment.objects.filter(user=appoint_user, seat=appoint_seat, start_appointment=appoint_start_appointment, end_appointment=appoint_end_appointment).exists():
                messages.warning(request, "This appointment already exists.")

            else:
                patient = Appointment.objects.filter(user=appoint_user)
                if patient.count() > 0:
                    patient[0].user.is_patient = 'Patient'
                    patient.user.save()
                
                else:
                    form.save()

>Solution :

try this

patients = Appointment.objects.filter(user=appoint_user)
if patients.count() > 0:
  for patient in patients:
      patient.user.is_patient = 'Patient'
      patient.user.save()

or

patients = Appointment.objects.filter(user=appoint_user)
if patients.count() > 0:
    appoint_user.is_patient = 'Patient'
    appoint_user.save()
  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading