Opened 5 years ago

Closed 5 years ago

#30972 closed New feature (duplicate)

USERNAME_FIELD UniqueConstraint option

Reported by: Iman Kermani Owned by: nobody
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords: AbstractUser UniqueConstraint
Cc: Iman Kermani Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm having a case that the User model USERNAME_FIELD is cellphone and at registration, cellphone should be verified with SMS OTP. Meanwhile, the User object should be created with is_verified = False , so in Meta class, I need a UniqueConstraint like the following code:

UniqueConstraint(fields=["cellphone"], condition=Q(is_verified=True), name="unique_is_active_cellphone")

Generally, I need to have the ability to create multiple users with the same username and have a unique Active or Verified in the system.

Change History (2)

comment:1 by Iman Kermani, 5 years ago

Cc: Iman Kermani added

comment:2 by Mariusz Felisiak, 5 years ago

Component: contrib.authDatabase layer (models, ORM)
Resolution: duplicate
Status: newclosed
Version: 2.23.0

On PostgreSQL you should be able to implement this with ExclusionConstraint (available from Django 3.0). As far as I'm concerned they are not available on other databases.

Duplicate of #29824.

Note: See TracTickets for help on using tickets.
Back to Top