2016-08-15 2 views
0

프로덕션 데이터베이스에 기존 국가 클래스가 있지만 사용하는 모든 국가를 업데이트하는 데 Django_Countries 모델을 사용하고 싶습니다.django 국가를 기존 모델에 통합하는 방법

다음은 기존 모델입니다. 그것은 사용자가 국가를 만들고 내가 국가를 만드는 사용자의 옵션을 제거하고 싶은 국가를 선택이 그들의 국가

class Country(models.Model): 
    name = models.CharField(_("Name")) 
    icon = models.ImageField(_("Icon"),upload_to=os.path.join('images', 'flags')) 

의 깃발을 업로드 할 수 있습니다.

많은 의존성이 있기 때문에 기존 모델을 정말로 바꿀 수 없습니다. 기존 모델에 이름과 플래그를 추가하기 만하면됩니다.

+0

일회 이전으로 하시겠습니까? –

+0

빠른 1 라인 답을 찾으려는 느낌이 들지만, 존재하지도 않는 새로운 국가 모델을 사용하기 위해 참조를 변경해야합니다. – Sayse

답변

0

마지막으로 json 파일의 국가를 덤프하고 데이터베이스를 업데이트하기 위해 마이그레이션을 작성하기 만하면됩니다.

def forwards(apps, schema_editor): 
    Country = apps.get_model('country', 'Country') 
    Region = apps.get_model('country', 'Region') 

    Region.objects.filter(id=45).delete() 

    with codecs.open(os.path.join(os.path.dirname(__file__), 'countries.json'), encoding='utf-8') as cfile: 
    data = json.load(cfile) 

    for country, regions in data.items(): 
     country, created = Country.objects.get_or_create(name=country) 


class Migration(migrations.Migration): 
    dependencies = [ 
    ('country', 'previous_migration'), 
] 

operations = [ 
    migrations.RunPython(forwards), 
] 
관련 문제