python - Django 模型使用保留 Python 关键字作为数据库列名称

标签 python django django-models

我正在开发一个 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/

相关文章:

python - 如何根据django中模型其他字段的状态修改字段渲染行为

django - 使用 Django QuerySet 分块处理数据库的最佳方法?

javascript - 我可以将 javascript 与 bottle(框架)一起使用吗?

python - 忽略稀疏矩阵中的重复条目

python - Django 和 Ajax

css - django-mediagenerator CSS 错误

python - Django 。如何在html <tr><td>name</td><td>value</td><td>date</td></tr>标签中显示json文件?

python - python方法在没有return语句的情况下的行为

Python GTK - 属性错误 : 'gi.repository.Gtk' object has no attribute 'Stack'

python - Django 可编辑文本字段