gpt4 book ai didi

Django 。将字段从模型映射到数据库表中的字段

转载 作者:行者123 更新时间:2023-12-02 08:16:32 26 4
gpt4 key购买 nike

我有以下模型:

class MyModel(models.Model):
field_one = models.CharField(max_length=255)
field_two = models.CharField(max_length=255)

class Meta:
db_table = "super_table"

以及以下数据库表:
CREATE TABLE public.super_table
(
id integer NOT NULL DEFAULT nextval('super_table_id_seq'::regclass),
field_two character varying(255) NOT NULL,
field_three character varying(255) NOT NULL,
)

我可以以某种方式映射 field_one在模型中 field_three在我的 super_table ?

最佳答案

是的,您可以使用 db_column

Field.db_column 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 scene


class MyModel(models.Model):
field_one = models.CharField(max_length=255, db_column='field_three')
field_two = models.CharField(max_length=255)

class Meta:
db_table = "super_table"

关于 Django 。将字段从模型映射到数据库表中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41548356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com