How can I set something like id
which would be unique and generated after creating an object?
I would like to assign identifier for each object so user
can indentify product by this id but I don't want it to be regular autoincrement id because I want it let's say consists of 10 digit. (my pk's are 1,2...inf
).
What I've done so far is that I've tried to create a hash of the name but it's not a good way to do that.
class Job(models.Model):
job_id = models.BigIntegerField() # or hash(something)
customer = models.ForeignKey(User, related_name='orders', help_text=_("Customer"), on_delete=models.CASCADE)
translator = models.ForeignKey(User, related_name='jobs', null=True, blank=True, help_text=_(u"Translator"))
price = models.FloatField(null=True, blank=True, help_text=_(u"Price"))
One possible way would be to create a post_save
signal
which would generate random 10 digit number and then check for uniqueness but it's probably very consuming solution.