【问题标题】:django-graphene alter column name deprecationdjango-graphene 更改列名弃用
【发布时间】:2018-06-09 11:04:55
【问题描述】:

我想更改数据库表中的列名,弃用 django-graphene 中的旧字段并添加新字段。

如何或者可以在我的 Django 模型中不创建两次相同的列的情况下做到这一点?在执行此操作时,我可以避免系统检查期间出现错误,但我的测试仍然会出错。

型号

class MyModel(BaseModel):
    my_column = models.CharField(
        max_length=255, blank=True, null=True)
    mycolumn = models.CharField(
        max_length=255, blank=True, null=True
        db_column='my_column')

架构

class MyNode(DjangoObjectType):
    mycolumn = String(deprecation_reason='Deprecated')

设置

SILENCED_SYSTEM_CHECKS = ['models.E007']

这可行,但是,现在我尝试在创建示例 MyModel 工厂实例的地方运行测试。

class TestMyModel(TestModelBase):
    def setUp(self):
        self.my_model = MyModel(my_model_nm='Some model')

这当然会引发异常。

django.db.utils.ProgrammingError: column "my_column" specified more than once

我似乎做错了。如何更改 django-graphene 中的字段名称、弃用旧名称并让新字段引用表中的同一列?

石墨烯==1.2

石墨烯-django==1.2.1

graphql-core==1.0.1

【问题讨论】:

  • 您是要更改 Django 模型中的字段名称,还是仅在石墨烯 API 中更改字段名称,还是两者兼而有之?
  • 两者都是理想的,但特别是在 API 中。
  • 您是否尝试过为您的模型列更改创建迁移以先重命名它? docs.djangoproject.com/en/2.0/topics/migrations
  • 迁移与它没有任何关系,但无论如何它是一个非托管表。
  • 啊,好吧——我想我直到看到你的解决方案才明白你的问题:-)

标签: python django graphene-python


【解决方案1】:

这就是我们最终要做的事情。

from graphene import String
from graphene_django.converter import convert_django_field


class AliasCharField(models.Field):
    """
    Alias for a CharField so that two fields may point to the same column.
    source: https://djangosnippets.org/snippets/10440/
    """
    def contribute_to_class(self, cls, name, virtual_only=False):
        super(AliasCharField, self).contribute_to_class(cls, name,
                                                        virtual_only=True)
        setattr(cls, name, self)

    def __get__(self, instance, instance_type=None):
        return getattr(instance, self.db_column)


@convert_django_field.register(AliasCharField)
def convert_alias_char_field_to_string(field, registry=None):
    """
    Tell graphene-django how to deal with AliasCharField.
    source: https://github.com/graphql-python/graphene-django/issues/303
    """
    depr_reason = getattr(field, 'deprecation_reason', None)
    return String(description=field.help_text,
                  deprecation_reason=depr_reason,
                  required=not field.null)


class MyModel(BaseModel):
    my_column = models.CharField(
        max_length=255, blank=True, null=True)
    mycolumn = models.CharField(
        max_length=255, blank=True, null=True
        db_column='my_column')
    my_column.deprecation_reason = 'Deprecated'

这可以在不抑制系统检查设置的情况下工作。

【讨论】:

    猜你喜欢
    • 2017-12-26
    • 2018-03-31
    • 1970-01-01
    • 2020-03-29
    • 2014-07-23
    • 2020-08-02
    • 2017-01-15
    • 2023-03-29
    • 2019-12-20
    相关资源
    最近更新 更多