0

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?

EarlyCoder
  • 1,213
  • 2
  • 18
  • 42
  • It should work if you put unique=True on project_name as well. If it doesn't work, the error is probably in the code for adding a subscription. Could you add that code to your question? – FrederikVds Oct 14 '15 at 15:16
  • @FrederikVds, I did try your solution and it gives me and the form reloads complaining that "User with this Email address already exists." Obviously, when email is still unique, you cannot register the same email address again. Adding unique = True for project_name only enforces uniqueness for project_name, but does nothing to accept a duplicate email. – EarlyCoder Oct 15 '15 at 13:04
  • Yes, but what is the code for the form that generates this error? Your models seem fine, but you should not try to create a new User in your form, you should try to find the existing User and only add a new Subscription. – FrederikVds Oct 15 '15 at 15:32

1 Answers1

0

Well, the way I solved it was to catch the exception. When it throws the exception saying user already exists after a call to is_valid(), I make sure I just register the project.

EarlyCoder
  • 1,213
  • 2
  • 18
  • 42