【问题标题】:How to create relation on in django User model and other model if other model have two fields to make relation如果其他模型有两个字段来建立关系,如何在 django 用户模型和其他模型中创建关系
【发布时间】:2021-11-14 21:27:00
【问题描述】:

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

当我尝试使用外键时

约会模式

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:\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'.

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

我如何与用户预约的患者和医生建立关系?

更新


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

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)

【问题讨论】:

    标签: python django django-models foreign-keys many-to-many


    【解决方案1】:

    更改这些行

    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”不是有效用户。

    【讨论】:

    • 感谢它的工作,但我使用外键和用户模型及其工作我已经更新了问题,让我知道这是否正确。
    • 是的,这对我来说似乎是正确的..
    猜你喜欢
    • 2020-03-30
    • 2018-06-24
    • 2020-02-12
    • 1970-01-01
    • 2021-09-25
    • 2021-12-20
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    相关资源
    最近更新 更多