I'm building a multi-tenant application with a data model that simplifies 
to:

class Tenant(models.Model):
    name = models.CharField(max_length=50)

class Widget(models.Model):
    owner = models.ForeignKey(Tenant)
    sequence_id = <question here>


I want Widget.sequence_id to work like an AutoField (that is, automatically 
give me something unique and generally incrementing by 1) but scoped to a 
single Widget.owner. So for instance, when Tenant A creates 3 widgets, they 
would get sequence_ids 1, 2, and 3. Then Tenant B comes along and creates 2 
widgets; those widgets get sequence_ids 1 and 2. The tenant/user gets no 
control over the number, but it's something she'll see in the UI. 
AutoNumber is out because from the perspective of a single tenant, they 
should have sequential widget IDs. GUIDs/UUIDs are out because it needs to 
be human-readable.

I looked through docs on AutoField and unique_together. I turned 
StackOverflow upside down and only found this 
<http://stackoverflow.com/questions/18072586/django-autofield-increment>, 
which isn't quite what I want. I didn't see anything on djangopackages.com 
that would solve my problem, though I could have missed it.

Basically, has anyone done something like this before and have a suggested 
pattern?

Thanks!
~matt

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/37fd32e7-28dd-4c10-8451-b0d705a3e52e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to