如何在django中创建User模型和其他模型上的关系,如果其他模型有两个字段来创建关系



我正在开发一个应用程序,其中我有两种类型的用户Patients和Doctors,患者可以在其中预约医生。我使用Django的内置用户模型来注册用户。我有一个预约模型,但是我无法建立预约模型和用户模型之间的关系,因为我想在预约模型中引用病人和医生。

当我尝试使用外键

时<

约会模型/h2>
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)

迁移期间

python manage.py migrate

误差

(venv) D:PythonDjangoFYPHeatlhCare>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:PythonDjangoFYPvenvlibsite-packagesdjangodbmodelsfields__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:PythonDjangoFYPHeatlhCaremanage.py", line 22, in <module>
main()
File "D:PythonDjangoFYPHeatlhCaremanage.py", line 18, in main
execute_from_command_line(sys.argv)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangocoremanagement__init__.py", line 419, in execute_from_command_line
utility.execute()
File "D:PythonDjangoFYPvenvlibsite-packagesdjangocoremanagement__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangocoremanagementbase.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangocoremanagementbase.py", line 398, in execute
output = self.handle(*args, **options)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangocoremanagementbase.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangocoremanagementcommandsmigrate.py", line 244, in handle
post_migrate_state = executor.migrate(
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbmigrationsexecutor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbmigrationsexecutor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbmigrationsexecutor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbmigrationsmigration.py", line 126, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbmigrationsoperationsfields.py", line 244, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbbackendsbaseschema.py", line 608, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
File "D:PythonDjangoFYPvenvlibsite-packagesmssqlschema.py", line 383, in _alter_field
new_default = self.effective_default(new_field)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbbackendsbaseschema.py", line 324, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbmodelsfieldsrelated.py", line 971, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbmodelsfields__init__.py", line 842, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbmodelsfields__init__.py", line 2486, in get_db_prep_value
value = self.get_prep_value(value)
File "D:PythonDjangoFYPvenvlibsite-packagesdjangodbmodelsfields__init__.py", line 1825, in get_prep_value
raise e.__class__(
ValueError: Field 'id' expected a number but got 'None'.

通过使用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)

迁移成功,注册一些用户并添加约会但这是一个问题,当我创建一个约会,如在它patient_userdoctor_user是可迭代对象,而不仅仅是单个对象

# 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)

我如何在预约中与患者和医生建立关系?

更新

在指定中进行更改,现在它正在工作

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)

修改这些行

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

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

然后清除最后一个迁移文件并再次进行迁移

你得到这个错误,因为UsersInfo或DoctorsInfo只能有有效的数据作为默认值,"None"不是一个有效的用户。

最新更新