So, I have these two models:
class User(AbstractBaseUser):
"""
Custom user class
"""
email = models.EmailField(verbose_name = 'email address', unique = True, db_index = True)
joined = models.DateTimeField(auto_now_add = True)
is_active = models.BooleanField(default = True)
is_admin = models.BooleanField(default = False)
user_type = models.CharField(max_length = 30)
group = models.ManyToManyField(Group, related_name = 'users')
permission = models.ManyToManyField(Permission, related_name = 'users')
USERNAME_FIELD = 'email' # Mapping between our model's email field to Django's User model's username
and
class Subscription(models.Model):
project_name = models.CharField(max_length = 40) # A subscription can only be for one project (one to one), e.g. you cannot buy a subscription for two projects. You have to buy two different subscriptions
user = models.ManyToManyField(User, related_name = 'subscriptions', blank = True) # One user can have many different subscriptions (e.g. for different projects), and one subscription is good for many users
date = models.DateTimeField(auto_now_add = True) # Keeping track of when a subscription is entered
mop = models.CharField(max_length = 20, choices = PAYMENT_CHOICES)
payment_term = models.CharField(max_length = 30, choices = PAYMENT_TERMS)
package = models.CharField(max_length = 50, choices = PACKAGES)
So, there is a many-to-many relation between user and subscription, e.g. one user can have many subscriptions and one subscription is good for many users.
When the user registers, it should allow the user to register with the same email address for different projects. But, it does not do that because unique=True is set. I tried removing the unique=True, but apparently it complains that Django needs a unique field for USERNAME = 'email'.
What I ideally want is a unique=True on both the email (of User model) and project (of Subscription model), e.g. although a user cannot register a project more than once, but a user can have many project. How do I do that? It seems the project_name field belongs in the subscription model and not in the user model since if it were in the user model, because of the many-to-many relationship, that meant that a subscription can have many projects which is not right (i.e. a subscription is always for one project).
On one hand, I want the login page allow the user to login just using their email address (thus I need unique=True on email for login). On the other hand, I want the registration to allow the same email to be registered with different projects and not complain about this email already exists (thus, I do NOT need unique=True on email for registration).
Any ideas?