Py学习  »  Django

Django约束-只允许一个具有相同字段的非草稿状态对象

Milano • 2 年前 • 751 次点击  

我有个模特 Application 带有字段 bank , mortgage status .相同 银行 抵押贷款 我只允许一个 application 具有 地位 与DRAFT不同。

class Application(..):
    bank = ..
    mortgage = ..
    status = .. # draft, active, in_progress etc.

因此,可以有多个这样的应用程序:

# ok
Application(bank=1, mortgage=2, status='draft') 

# ok, tuple mortgage and bank already exists but but status is draft
Application(bank=1, mortgage=2, status='draft') 

# ok, tuple mortgage and bank already exists but status is active
Application(bank=1, mortgage=2, status='active') 

# ERROR - tuple mortgage and bank already exists and there is already one object with non-draft status
Application(bank=1, mortgage=2, status='in_progress') 

# ok - bank is different
Application(bank=3, mortgage=2, status='active') 

换句话说,我不能创建一个具有相同银行、相同抵押贷款和状态的应用程序 draft 如果已经有一个具有相同银行和抵押贷款的申请,并且状态不是 草稿

有可能做这样的约束吗?将 CheckConstraint 工作

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/159351
 
751 次点击  
文章 [ 1 ]  |  最新文章 2 年前