0

I created two models "Category" and "Item". There is a field "title" in category model, I want the value of the "title field" to be prefix of my "unique id field" in Item module can anyone suggest me a solution for this problem.

Thank you

I.SRI RAM
  • 15
  • 7

1 Answers1

0

The default id in django models is something like this:

class Custom(models.Model):
    id = models.IntegerField(primary_key=True, editable=False)

So, the possible way to achieve the requirement is to change the IntegerField to CharField.

Technically you can use "String PK Field" But, you should be aware of the problems/performance issues if you are going to use that. more: Strings as Primary Keys in SQL Database

If you still really wish to migrate to String PKs:

First you need to use the CharField instead of IntegerField and override the save() method of model

from django.db.models import Max


class Item(models.Model):
    id = models.CharField(primary_key=True, editable=False)
    prefix = models.CharField(max_length=100)

    def save(self, **kwargs):
        if not self.id:
            max = Item.objects.aggregate(id_max=Max('id'))['id_max']
            self.id = "{}{:05d}".format(self.prefix, max if max is not None else 1)
        super().save(*kwargs)

According to this: Django generate custom ID

Hamid Rasti
  • 813
  • 1
  • 6
  • 16
  • Thank you for the solution I will let you know if it works, Thank you – I.SRI RAM Dec 24 '20 at 08:11
  • the 'id' you mentioned in "if statement" is from "Item model" right – I.SRI RAM Dec 24 '20 at 08:21
  • yes it is. and you can set `max_length=10` or `max_length=100` in the field declaration. because I do not know your category `title` max length I dont set max_length for Item `id` – Hamid Rasti Dec 24 '20 at 08:45
  • you should set `max_length` of `id` and `prefix` based on your requirement – Hamid Rasti Dec 24 '20 at 08:49
  • Ok thank you, I set my id and prefix, Can we add value which is stored in title as prefix here. – I.SRI RAM Dec 24 '20 at 09:22
  • @I.SRIRAM Yes we can. We may have many approches for this requirement. you can define custom model field for this requirement. read this: [link](https://docs.djangoproject.com/en/3.1/howto/custom-model-fields/) – Hamid Rasti Dec 24 '20 at 09:50
  • My Category Model ``class Category(models.Model): cat_title = models.CharField(max_length=100)`` And in my Item Model ``product_id = models.CharField(primary_key=True, editable=False, max_length=100) prefix = models.OneToOneField(Category, to_field='title', related_name='prefix', on_delete=models.CASCADE) def save(self, **kwargs): if not self.product_id: max = Item.objects.aggregate(id_max=Max('product_id'))['id_max'] self.product_id = "{}{:10}".format(self.prefix, max if max is not None else 1) super().save(*kwargs)`` – I.SRI RAM Jan 04 '21 at 10:21
  • As per the above code. I used cat_title from category model as prefix to the unique id which i used in Item model, I can generate unique id with prefix as cat title only once because it is requesting to create cat_title field to be unique,So if i add unique=True in Category model, I can only save a single item with unique prefix can you help me with it. Thank You. – I.SRI RAM Jan 04 '21 at 10:25