Перенос данных с юга на новую модель не работает - PullRequest
0 голосов
/ 01 февраля 2012

У меня есть абстрактная модель, которую я конвертирую в конкретную модель. Я успешно использую юг для изменения схемы, но не могу использовать миграцию данных.

Мое начальное состояние:

class UserProfile(models.Model):
    user = models.OneToOneField(User, primary_key=True, \
                                related_name='profile')
    class Meta:
        abstract=True
class SpecificProfile(UserProfile):
    url = models.URLField()

Мое новое состояние:

class UserProfile(models.Model):
    user = models.OneToOneField(User, primary_key=True, \
                                related_name='profile')
class SpecificProfile(UserProfile):
    user_profile = models.OneToOneField(UserProfile, parent_link=True)
    url = models.URLField()

Моя миграция схемы:

class Migration(SchemaMigration):

    def forwards(self, orm):
        # Renaming field 'SpecProfile.user_profile'
        db.rename_column('specificprofile', 'user_id', 'user_profile_id')

        # Adding model 'UserProfile'
        db.create_table('userprofile', (
            ('user', self.gf('django.db.models.fields.related.OneToOneField')(related_name='profile', unique=True, primary_key=True, to=orm['auth.User'])),
        ))
        db.send_create_signal('myapp', ['UserProfile'])

Я отредактировал файл, созданный на юге, чтобы переименовать одно поле в SpecificProfile

Теперь, в процессе переноса данных, я хотел бы создать одну запись UserProfile для SpecificProfile и назначить UserProfile.user_id для SpecificProfile.user_profile_id.

Итак, мои перенаправления данных:

class Migration(DataMigration):

    def forwards(self, orm):
        for spec in orm.SpecificProfile.objects.all():
            user_profile = orm.UserProfile()
            user_profile.user_id = spec.user_profile_id
            user_profile.save()

Сценарий выполняется без ошибок, но не создает никакой новой записи в таблице UserProfile. Должен ли я использовать UserProfile() вместо orm.UserProfile()?

Есть идеи?

1 Ответ

0 голосов
/ 01 февраля 2012

SpecificProfile.user_profile_id ранее не существовало, поэтому у него нет данных для переноса. Что вы действительно хотите сделать, это установить user_profile.user на spec.user, а затем установить spec.user_profile на user_profile.

 def forwards(self, orm):
    for spec in orm.SpecificProfile.objects.all():
        user_profile = orm.UserProfile()
        user_profile.user_id = spec.user_id
        user_profile.save()

        # Then,
        spec.user_profile_id = user_profile.id

Однако, как только вы сделали начальную миграцию, я почти уверен, что SpecificProfile.user больше не существует. Юг удаляет это поле, так как оно теперь на UserProfile.

...