2017-12-28 4 views
0

私のデータベースのテーブルの列名を変更したい、django-grapheneの古いフィールドを非難し、新しいフィールドを追加したい。django-graphene列名の廃止を変更

Djangoモデルで同じ列を2回作成しなくても、これをどうやって行うことができますか?これを実行中にシステムチェック中にエラーを回避することはできますが、テストではエラーが発生します。

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

設定

SILENCED_SYSTEM_CHECKS = ['models.E007'] 

この作品

モデル

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') 

スキーマは、しかし、今、私はサンプル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

グラフェンジャンゴ== 1.2.1

graphqlコア== 1.0.1

+0

Djangoモデルのフィールド名を変更しようとしていますか、グラフェンAPIだけ、あるいはその両方ですか? –

+0

どちらも理想的ですが、具体的にはAPIにあります。 – duffn

+0

モデルの列変更のマイグレーションを作成して名前を変更しようとしましたか? https://docs.djangoproject.com/en/2.0/topics/migrations/ –

答えて

0

は、ここで我々がやってしまったものです。

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' 

これは、システムチェックインの設定を抑制することなく機能します。

関連する問題