Django using unique_together in a subclass of custom user model

531 views Asked by At

I extended Django's user class by extending the abstract first:

class XUserAbstract(AbstractBaseUser, PermissionsMixin):
     email = models.EmailField(
          db_index=True,
          verbose_name='email address',
          max_length=255
     )    
     # ...
     class Meta:
     verbose_name = "User"
     verbose_name_plural = "Users"
     abstract = True

Then, I created a subclass from this abstract by:

class XUser(XUserAbstract):

    class Meta(XUserAbstract.Meta):
    swappable = 'AUTH_USER_MODEL'

After I had my own user class, I created a subclass from it and created a FK to Application model:

class Merchant(XUser):
    app = models.ForeignKey(Application)
    # ...
    class Meta:
        verbose_name = _('merchant')

The problem is that when I wanted to create a unique pair inside Merchant, I could not manage to do it. I wanted to create a unique pair with Merchant.app and XUser.email I looked for the answer but the solution was not fixing my case because I inherited User class as well from its abstract (?). The solution to add unique pair for a subclass I found was:

unique_together = ('app', 'user__email')

But it does not work. The error:

main.business: "unique_together" refers to user__email, a field that doesn't exist.

I tried those as well:

    unique_together = ('app', 'xuser__email')
    unique_together = ('app', 'email')

The second line gives this error:

main.business: "unique_together" refers to email. This is not in the same model as the unique_together statement.

They don't also work.

Is there something I'm missing?

0

There are 0 answers