django-graphene 改变列名称弃用
django-graphene alter column name deprecation
我想更改 table 我的数据库中的列名称,弃用 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 中的字段名称,弃用旧名称并让新字段引用我的 table?
中的同一列
石墨烯==1.2
石墨烯-django==1.2.1
graphql-core==1.0.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'
这可以在不抑制系统检查设置的情况下工作。
我想更改 table 我的数据库中的列名称,弃用 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 中的字段名称,弃用旧名称并让新字段引用我的 table?
中的同一列石墨烯==1.2
石墨烯-django==1.2.1
graphql-core==1.0.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'
这可以在不抑制系统检查设置的情况下工作。