Opened 8 years ago

Last modified 8 years ago

#27311 closed New feature

Assigning ForeignKey fields in migrations — at Version 3

Reported by: Odero Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords: migrations, foreignkey, cache
Cc: Markus Holtermann Triage Stage: Someday/Maybe
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

Trying to create the migration below which is failing. It seems like the same issue discussed and resolved in #24282.

def generate_invoices(apps, schema_editor):
    A = apps.get_model('app1', 'A')
    B = apps.get_model('app1', 'B')
    User = apps.get_model('accounts', 'User')

    a = A.objects.last()

    b = B(
        client=a.user,
        status=5
    )
    b.save()

Also tried

    b = B(
        client=User.objects.get(pk=a.user.pk),
        status=5
    )

In both cases I get the error:

Cannot assign "<User: Some User>": "B.client" must be a "User" instance.

Change History (3)

comment:1 by Odero, 8 years ago

Description: modified (diff)

comment:2 by Odero, 8 years ago

Description: modified (diff)

comment:3 by Tim Graham, 8 years ago

Component: UncategorizedMigrations
Description: modified (diff)

I can't reproduce this. Could you provide a sample project and confirm your Django version?

Note: See TracTickets for help on using tickets.
Back to Top