[Answer]-Concatenate auto field with string using django

1👍

  1. Create field id as charfield e.g.

string_id = models.CharField(max_length=100, default='file')

  1. Make a function that make string id when the model is saved.

    def save(self):
    new_id = self.id
    self.string_id = str(new_id) + '_dot'
    super(YourModelName, self).save()
👤Rieven

0👍

You need to call super() first.

As you can read in the documentation:

There’s no way to tell what the value of an ID will be before you call save(), because that value is calculated by your database, not by Django.

When you call super().save(), the database engine calculates the id (or gid in your case, but please reconsider using the provided id) and thus allows you to use it. Before that it is None.

You may check this topic as well.

👤dsoosh

Leave a comment