﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25771	Serialization of natural foreign key in migration scripts does not work	Bowen Song	nobody	"The `serializers.serialize(..., use_natural_foreign_keys=True)` is not working in migration scripts, the `ManyToManyField` always have the value of the `pk` instead of the `natural_key`

An example to reproduce this bug:

models.py
{{{
# -*- coding: utf-8 -*-
from django.db import models


class Student(models.Model):
    student_id = models.CharField(max_length=10, unique=True)
    name = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name

    def natural_key(self):
        return self.student_id


class Teacher(models.Model):
    name = models.CharField(max_length=30)
    students = models.ManyToManyField(Student)

    def __unicode__(self):
        return self.name
}}}


migrations/0002_example.py
{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
from django.core import serializers


def example(apps, schema_editor):
    Teacher = apps.get_model(""mytest"", ""Teacher"")
    json_data = serializers.serialize('json', Teacher.objects.all(), use_natural_foreign_keys=True)
    print json_data


class Migration(migrations.Migration):

    dependencies = [
        ('mytest', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(example, reverse_code=migrations.RunPython.noop)
    ]
}}}

I applied the initial migration, and inserted some data into those tables.

The dumpdata command output is:
{{{
$ python manage.py dumpdata mytest --natural-foreign 
[{""fields"": {""student_id"": ""1234567890"", ""name"": ""Alice""}, ""model"": ""mytest.student"", ""pk"": 1}, {""fields"": {""student_id"": ""9876543210"", ""name"": ""Bob""}, ""model"": ""mytest.student"", ""pk"": 2}, {""fields"": {""students"": [""1234567890"", ""9876543210""], ""name"": ""Petter""}, ""model"": ""mytest.teacher"", ""pk"": 1}]
}}}

When running the migrate command, output is:
{{{
$ python manage.py migrate mytest 0002
Operations to perform:
  Target specific migration: 0002_example, from mytest
Running migrations:
  Rendering model states... DONE
  Applying mytest.0002_example...[{""fields"": {""students"": [1, 2], ""name"": ""Petter""}, ""model"": ""mytest.teacher"", ""pk"": 1}]
 OK
}}}

Expected output is:
{{{
$ python manage.py migrate mytest 0002
Operations to perform:
  Target specific migration: 0002_example, from mytest
Running migrations:
  Rendering model states... DONE
  Applying mytest.0002_example...[{""fields"": {""students"": [""1234567890"", ""9876543210""], ""name"": ""Petter""}, ""model"": ""mytest.teacher"", ""pk"": 1}]
 OK
}}}"	Bug	closed	Uncategorized	1.8	Normal	invalid	database migration serialization		Unreviewed	0	0	0	0	0	0
