Django 模型使用 reserve Python 关键字作为数据库列名称

Django Model using reserve Python keyword for DB columns name

我正在开发 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.