对DB columns nam使用reserve Python关键字的Django模型

2024-04-19 21:20:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个Django restfulapi&我的DB coulmn名称(即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)

**我希望在不重命名DB列名的情况下执行此操作

有什么建议吗?在


Tags: 文件djangolambda代码py名称defaultdb
1条回答
网友
1楼 · 发布于 2024-04-19 21:20:20

您可以使用^{}来进行相同的操作。根据文件,它应该工作:

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.

相关问题 更多 >