Edited
It seems that the original solution does not work, Django (v. 1.11) requires an autofield field to have the parameter primary_key=True, an error, in my view, for what is the parameter? The way is to try to remedy with a function, see if it works, the function can be put in the file itself models or in another separate file (as long as you import it in models.py)
import datetime
def increment_chamado():
ultimo = Chamado.objects.all().order_by('id').last()
if not ultimo:
return str(datetime.date.today().year) + str(datetime.date.today().month).zfill(2) + '0000'
ultimo_id = ultimo.chamado_id
ultimo_int = int(ultimo_id[9:13])
new_int = ultimo_int+1
new_id = str(str(datetime.date.today().year)) + str(datetime.date.today().month).zfill(2) + str(new_int).zfill(4)
return new_id
In models the field that identifies the call would be:
classe Chamado(models.Model):
chamado_id = models.CharField(max_length = 20, default =
increment_chamado, editable=False)
Based on a post from thecstream.org.
From this point on, the answer "original".
Autofield
Integer field with automatic increment, Primary keys use it automatically. A primary key is automatically added to your template if you do not specify it.
If you add to your template:
incremento = models.AutoField(primary_key=False)
You will be adding a field of the type you want (auto-increment) to your model.
SQN
Only... if you haven’t defined a PK of your own autofield, when you try to spin the migrations to add your field to the bank, you will receive the msg:
AssertionError: A model can't have more than one AutoField.
Solution
Define your own PK on a field that is not autofield
Suggestion
If you don’t have a field that can guarantee it unique to use as PK make the PKas being uuid thus:
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Transferring the id value to a document field is a bad idea, because that way you will be exposing the bank’s hold unnecessarily.
– Sidon