Auto Create Alpha Numeric ID for Django Model

邮差的信 提交于 2020-01-07 07:13:06

问题


Just like how Django models automatically create an integer ID for each column entry of a database table, I would like it to automaticallty create an alpha numeric ID as well.

The integer can increment, while the character should be cycle through A-Z.

1_A
2_B
3_C
.
.
.
27_A
28_B

And so on.

Here is my models.py

from django.db import models
class Sample(models.Model):
    sample_ID = models.CharField(max_length=20) # Alpha numeric ID described above.
    sample_name = models.CharField(max_length=30)

As you can tell, this will produce a blank text field for users to enter this value in, though I would like it to be auto generated just as with the integer id. This will then be a fixed ID.

What confuses me about this is that I don't know how to incorporate customized/automated fields into a model (I am quite new to Django). Is there already a field that can do this? Or how can I make a custom field?

Django = 1.8

python = 2.7

Thank you

来源:https://stackoverflow.com/questions/31594386/auto-create-alpha-numeric-id-for-django-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!