views:

84

answers:

2

This is what I had before (but realized that you can't obviously do it in this order:

class MasterAdmin(models.Model):
    """
    A permanent admin (one per Account) that shouldn't be deleted.
    """
    admin = models.OneToOneField(AccountAdmin)

class Account(models.Model):
    """
    A top-level account in the system.
    """
    masteradmin = models.OneToOneField(MasterAdmin)


class AccountAdmin(models.Model):
    """
    An Account admin that can be deleted.  This includes limited permissions.
    """
    account = models.ForeignKey(Account)

I think you can see what I want to do from the example. I want to have an MasterAccountAdmin which shares the attributes from AccountAdmin. The purpose is that I want to give people the ability to delete an AccountAdmin, but not MasterAccountAdmin. I didn't want to just have an attribute on AccountAdmin called "master = models.BooleanField()".

Obviously this example won't work because MasterAdmin is referencing AccountAdmin before its creation, but I wanted to show what I'm trying to achieve. Am I thinking of this all wrong?

+3  A: 

Why not just make is_master a property of AccountAdmin and then override the delete() method to ensure is_master is not true?

Tom
This is a better approach than what the OP is doing, but I wanted to answer his question related to forward references.
celopes
Oh, good point. I was so busy with the architecture I didn't see the question (and that was an issue I ran into when first working in Django too).
Tom
+2  A: 

When you have forward references, use the quotes.

admin = models.OneToOneField('AccountAdmin')

See the docs.

If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself...

celopes
Thanks! BTW, is this very Pythonic to do (this in quotes thing)?
orokusaki
It's a special feature of django, to accommodate this kind of relationship. It's perfectly acceptable practice, but it isn't supported by python explicitly.
jcdyer
What jcd said. I updated my answer to have a link to the docs.
celopes