I have an app that lets people create tickets, and each Account owner using my app can create tickets.
Firstly, should it be unique per account do you think? Or, should it be completely unique?
Like this:
class Ticket(models.Model):
"""
An ticket created by an Account holder.
"""
account = models.OneToOneField('Account')
number = models.CharField(max_length=20, verbose_name='Ticket Number')
class Meta:
unique_together = (('account', 'number'),)
or this, respectively:
class Ticket(models.Model):
"""
An ticket created by an Account holder.
"""
account = models.OneToOneField('Account')
number = models.CharField(unique=True, max_length=20, verbose_name='Ticket Number')
Then, of course I want the order numbers to represent the account, so possibly a sample order number for an account that has the id: “chris” could be “chris-41984” or something like that. Would you create a model method for this, or…?
If users can create more than one ticket, then a one-to-one field is not appropriate. Secondly, the ticket number is purely arbitrary, right? In other words, it only needs to be unique (I don’t see any reason why it should be unique per user from what you have said so far), so you might as well just use the primary key, unless you’re planning on integrating with some other data store.
So you can simply say:
Note that if you uncomment the
numberdefinition, you’ll need to specify it when you create the model instance – primary keys cannot beNone. Alternately, override the save method to auto-generate the ticketnumberfield based on your requirements.