我正在开发一个 Django Restful API,我的数据库库名(即 lambda 与保留的 python 关键字冲突),我需要在我的 model.py
文件中使用它有代码
CHOICES = (
(1, 'Default'),
(0, 'Not Default'),
)
profileid = models.AutoField(primary_key=True)
alpha = models.FloatField()
beta = models.FloatField()
gamma = models.FloatField()
lambda = models.FloatField() # <---------- How to use lambda for column name
is_default = models.IntegerField(choices=CHOICES)
** 我想要这个而不重命名数据库列名
有什么建议吗?
最佳答案
您可以使用 Field.db_column
对于相同的。根据文档,它应该可以工作:
my_lambda = models.FloatField(db_column='lambda')
The name of the database column to use for this field. If this isn’t given, Django will use the field’s name.
If your database column name is an SQL reserved word, or contains characters that aren’t allowed in Python variable names – notably, the hyphen – that’s OK. Django quotes column and table names behind the scenes.
关于python - Django 模型使用保留 Python 关键字作为数据库列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466362/