views:

119

answers:

1

I was wondering if there is already a way to create a separate autoincrement-ID-per-user field in Django?

Basically, I'm storing many related models and I need the IDs generated to be autoincrement per user.

I don't want to change how id works, just need a new field that I can add which is unique=True per user.

Any suggestions (other than overriding save and implementing it myself)?

+1  A: 

No, there's no such field, but I wonder why you think you need it. The ID is really just for the model's internal use, you shouldn't ever care what it is.

For example, if you want to know how many related items there are for a user then you would just use the count() method on the related queryset. If you want something to be unique per user, you can use the unique_together meta property.

Can you given an example of a use case for a per-user unique id?

Edited in response to comments: to get the object from a URL as you mention, you just need to do:

myuser.myobject_set.all()[7]
Daniel Roseman
One obvious use might be in urls. You might want urls like /username/asset/7/ , with the 7 part autoincrementing per user.
uswaretech
My use case is exactly what @uswaretech mentioned :)
Swaroop C H