M
M
Maxim Tarabrin2019-03-13 10:42:44
Django
Maxim Tarabrin, 2019-03-13 10:42:44

Migrating existing model data to MPTTModel?

There is a data model. Installed django-mptt, but how do I now migrate data from the old model to the new MPTTModel?

Tag(models.Model):
    name = models.CharField(u'Название', max_length=255, unique=True)
    parent = models.ForeignKey('self', verbose_name='Родительский тег', null=True, blank=True, related_name='children')

do:
Tag(MPTTModel):
    name = models.CharField(u'Название', max_length=255, unique=True)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, verbose_name='Родительский тег', null=True, blank=True, related_name='children')

When I do makemigrations, I am asked to put in default values, but after migrate all tags turn into single-level tags, without nested tags.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-03-13
@padr1no

$ python manage.py shell

In [1]: from some_app.models import Tag
In [2]: Tag.objects.rebuild()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question