How to create relation on in django User model and other model if other model have two fields to make relation

Viewed 113

I am working on an application where I have two types of users Patients and Doctors, where patients can book appointments with doctors. I am using Django's built-in User model to register users. I have an appointment model but I am unable to make the relation between the appointment model and the user model as I want to reference the patient and doctor in the appointment model.

when I tried with Foreign Key

Appointment Model

class Appointment(models.Model):
    patient = models.ForeignKey(User,on_delete=models.CASCADE,default="None")
    doctor = models.ForeignKey(User,on_delete=models.CASCADE,default="None")
    doctor = models.CharField(max_length=20,blank=False)
    patient = models.CharField(max_length=20,blank=False)
    date = models.DateField(blank=False)
    time = models.TimeField(blank=False)
    status = models.CharField(default="pending",blank=True, max_length=20)

during migrate

python manage.py migrate

Error

(venv) D:\Python\Django\FYP\HeatlhCare>py manage.py migrate
Operations to perform:
  Apply all migrations: Users, admin, auth, contenttypes, sessions
Running migrations:
  Applying Users.0002_auto_20210921_1250...Traceback (most recent call last):
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1823, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'None'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "D:\Python\Django\FYP\HeatlhCare\manage.py", line 22, in <module>
    main()
  File "D:\Python\Django\FYP\HeatlhCare\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\core\management\base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\core\management\commands\migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\migrations\migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\migrations\operations\fields.py", line 244, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\backends\base\schema.py", line 608, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "D:\Python\Django\FYP\venv\lib\site-packages\mssql\schema.py", line 383, in _alter_field
    new_default = self.effective_default(new_field)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\backends\base\schema.py", line 324, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\models\fields\related.py", line 971, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\models\fields\__init__.py", line 842, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\models\fields\__init__.py", line 2486, in get_db_prep_value
    value = self.get_prep_value(value)
  File "D:\Python\Django\FYP\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1825, in get_prep_value
    raise e.__class__(
ValueError: Field 'id' expected a number but got 'None'.

by changing Foreign key with ManyToManyField

class Appointment(models.Model):

    patient = models.ManyToManyField(User, related_name="patient_user")
    doctor = models.ManyToManyField(User,related_name="doctor_user")
    date = models.DateField(blank=False)
    time = models.TimeField(blank=False)
    status = models.CharField(default="pending",blank=True, max_length=20)

migration successful and register some users and add appointments but here is the issue when I create an appointment as in it patient_user and doctor_user are iterables not just single objects

# user is the current user/patient who is making appointment
patient_user = User.objects.filter(id=request.user.id)
doctor_user = User.objects.filter(id=doctor_id)
        
# book the appointment
new_appointment = Appointment.objects.create(date=date, time=time, status = "pending")
new_appointment.patient.set(patient_user)
new_appointment.doctor.set(doctor_user)

How I make relation with patient and doctor in appointment to User?

Update


make changes in Appoint now it's working

class Appointment(models.Model):

    patient = models.ForeignKey(User,on_delete=models.CASCADE,related_name="appointment_patient")
    doctor = models.ForeignKey(User,on_delete=models.CASCADE,related_name="appointment_doctor")

    date = models.DateField(blank=False)
    time = models.TimeField(blank=False)
    status = models.CharField(default="pending",blank=True, max_length=20)
1 Answers

Change these lines

patient = models.ForeignKey(UsersInfo,on_delete=models.CASCADE,default="None")
doctor = models.ForeignKey(DoctorInfo,on_delete=models.CASCADE,default="None")

to

patient = models.ForeignKey(UsersInfo,on_delete=models.CASCADE,null=True, blank=True)
doctor = models.ForeignKey(DoctorInfo,on_delete=models.CASCADE,null=True, blank=True)

then clear your last migration file and make migrations again

You are getting this error because the UsersInfo or DoctorsInfo can have only valid data as the default value, "None" is not a valid User.

Related