views:

16

answers:

1

I have a simple-ish ownership design on one of my models. It can be owned by multiple people and current owners can add other people but they have to confirm the invite before they are treated as a real owner.

class MyOwnedThing(models.Model):
    owners = models.ManyToManyField(User, through='Ownership', related_name='othings')

    def is_owner(self, user):
        return user in self.owners

class Ownership(models.Model):
    user = models.ForeignKey(User)
    myownedthing = models.ForeignKey(MyOwnedThing)

    confirmed = models.BooleanField(default=False)

The problem is MyOwnedThing.is_owner needs to check that the owner had confirmed their invite. Is there a simple way of doing that or am I left doing a separate try/except around Ownership.objects.filter(user=u, myownedthing=mot, confirmed=True)?

A: 

I typically use association tables for this type of functionality. The following hasn't been tested but should give you a general idea of what I mean:

class Resource(models.Model):
    resource = models.TextField(max_length=255)

class ResourceUser(models.Model):
    owner = models.ForeignKey(User)
    resource = models.ForeignKey(Resource)

    def is_owner(self, user, res):
        return self.filter(self.owner=user).filter(self.resource=res)
Andrew Sledge