2017-12-28 6 views
0

테이블의 컬럼 이름을 데이터베이스로 변경하고 django-graphene의 이전 필드를 사용하지 말고 새 필드를 추가하십시오.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

그라 핀 장고 == 1.2.1

graphql 코어 == 1.0.1

+0

Django 모델의 필드 이름을 변경하고 있습니까, 아니면 단지 graphene 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' 

이 기능은 시스템 체크 인 설정을 억제하지 않고 작동합니다.

관련 문제