views:

134

answers:

1

In this:

class Administrator(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    account = models.ForeignKey(Account)

    class Meta:
        unique_together = (('account', 'self.user.username'),)

The self.user.username part is obviously incorrrect. However, in this:

class Administrator(User):
    account = models.ForeignKey(Account)

    class Meta:
        unique_together = (('account', 'username'),)

would that work since I'm inheriting from User? (I can't test it yet because there are too many elements out of place elsewhere). Can I use the first version with 'user.username' instead though? Or, should I use the second version?

+2  A: 

It would be

unique_together = (('account', 'user__username'),)

if I understand what you're trying to do. Note the double underscore. That's how you look at a foreign key's object's properties.

Tom
Ok, thanks Tom. That works perfectly. I'm scared to subclass User (don't know what effects might come down the road from this), so I'll use that instead.
orokusaki
Tom
@Tom I'm building a SAAS so Users won't be the highest level of instance in my software. Accounts will. This means that a User could be anything from an Administrator to an account holder's Client, etc. Each might have its own permissions (ie, an Account Administrator will be able to edit their account settings (like their PayPal login, or insert CSS to use for their forms, etc, and a client of an account holder will be able to log in and send messages to the account holder and the account holder will reply to them (private conversations)). Does this sound OK, or am I still missing something.
orokusaki
I get where you're coming from and I can see your approach. Probably works just fine. The only thing I would say is that the people/ agents/ bots logging in are effectively Users; what you're talking about is what kinds of permissions a user has. An Account can't log in, someone tied to the account logs in, right? So the permissions belong to the users. Accounts are just like Groups, a way to assign the same permission set to multiple users. I may be totally off-base on this though. It's Friday afternoon, after all.
Tom