Reputation: 2495
I am trying to add a Prefix(YNT) to the primary key in django model
Models.py
class testmodel(models.Model):
product_id = models.IntegerField(max_length=500, primary_key=True)
product_short_code = models.CharField(max_length=500, default=0, null=True)
How Can I do That ? also instead of starting the primary key from 1 can I start it from 1000 ?
I want the table to look like this
product_id product_short_code
YNT1000 PP001
YNT1001 PL023
Upvotes: 0
Views: 1972
Reputation: 5621
I think you should see the primary key as a internal Django mechanism to reference and link objects.
Then if you want another, human readable id you can always create a char field product_id
that would be as simple as 'YNT' + str(1000+id)
which you can save using the post_save signal. You can also add an index to said field to make querying faster if needed.
Example:
@receiver(post_save, sender=Product)
def set_product_id(instance, created, **_kwargs):
if created:
instance.product_id = 'YNT' + str(1000 + instance.id)
But still, your relations should be on the default pk and not on the id.
Upvotes: 0
Reputation: 2342
There are some approaches that you should follow to achieve this:
1. If Prefix is common for all the entries then you can create a field in your model and add the prefix while getting the value.
class testmodel(models.Model):
product_code = models.IntegerField(db_index=True)
product_short_code = models.CharField(max_length=500, default=0, null=True)
@property
def product_id(self):
return f"YNT{self.product_code}"
2. If Prefix is not common for all the entries then you can create a field in your model to store the prefix and add the prefix while getting the value.
class testmodel(models.Model):
product_code = models.IntegerField(db_index=True)
product_prefix = models.CharField(max_length=20, default='YNT')
product_short_code = models.CharField(max_length=500, default=0, null=True)
@property
def product_id(self):
return f"{self.product_prefix}{self.product_code}"
3. If you want to create CharField
as primary_key
then you
def create_id():
// code for creating unique ids. assuming the id will be stored in _id variable.
return f"YNT{_id}"
class testmodel(models.Model):
product_id = models.CharField(primary_key=True, max_length=20, default=create_id)
product_short_code = models.CharField(max_length=500, default=0, null=True)
With the 3rd approach you have to make sure the primary key is always unique.
Upvotes: 0
Reputation: 21446
I think, It is not a good idea using string as a primary key. You can use helper method like on the following.
class testmodel(models.Model):
product_short_code = models.CharField(max_length=500, default=0, null=True)
def product_id(self):
return f'YNT{self.id}'
Upvotes: 1
Reputation: 227
Please change IntegerField to CharField in testmodel class.
class testmodel(models.Model):
product_id = models.CharField(max_length=500, primary_key=True)
product_short_code = models.CharField(max_length=500, default=0, null=True)
If you want that your primary key start any given specific number. Please follow above link. How can my Model primary key start with a specific number?
Upvotes: 0