带有ManyToManyField的Django-Postgres排除约束

2024-06-24 13:26:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想对ManyToManyField使用Django排除约束。不幸的是,到目前为止,我的努力是徒劳的。 这是我的预约模式:

从django.contrib.postgres.constraints导入排除约束 从django.contrib.postgres.fields导入DateTimeRangeField,RangeOperators

class Appointment:
    patients = models.ManyToManyField(Patient, related_name='appointments' , blank=True )     
    datetimerange = DateTimeRangeField(null=False, blank = False )
    doctor = models.ForeignKey(Doctor, related_name='doctor_appointments') 

    class Meta: 
        constraints = [ 
            ExclusionConstraint(
                name='unique_doctor',
                expressions=[
                    ('datetimerange', RangeOperators.OVERLAPS),
                    ('doctor ', RangeOperators.EQUAL),

                ], 
            ), 
            ExclusionConstraint(
                name='unique_patients',
                expressions=[
                    ('datetimerange', RangeOperators.OVERLAPS),
                    ('patients', RangeOperators.CONTAINED_BY)
                
                ],
                condition= Q(is_archived=False) & Q(is_cancelled=False)                
        ) 
        ]

不幸的是,这不起作用。引用医生的第一个约束非常有效,但第二个约束在迁移过程中给了我这个错误:

return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column "patient_id" named in key does not exist

    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "patient_id" named in key does not exist

这件事困扰了我很长一段时间。谢谢你的帮助


Tags: djangonamefalsemodelspostgrescontribclassrelated