Create function which fill automatically a field model

Hello,

I use Django 3.0 and I would like to create a function which fill automatically a field in my models.
Can I do it directly in models.py ? How?

Thanks a lot

I think you’re after the default argument to fields: https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.Field.default

Hope that helps!

Thanks you for your answer. I know how to make a default value in charfield but I don’t know how to build function which returns value for my default field. In my case this function must retrieve last id on my model and must concataned it with a string and the final result must be affected to default charfield. Have you an example ? Help me plz

Making one field’s value default to something calculated from another field’s value is best done by overriding save() instead. See: https://docs.djangoproject.com/en/3.0/topics/db/models/#overriding-model-methods

1 Like