Opened 8 years ago

Last modified 8 years ago

#27311 closed New feature

Assigning ForeignKey fields in migrations — at Version 1

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 Odero)

Trying to create the migration below which is failing. It seems like the same issue discussed and resolved here https://code.djangoproject.com/ticket/24282

def generate_invoices(apps, schema_editor):
    Payment = apps.get_model('finance', 'Payment')
    Invoice = apps.get_model('finance', 'Invoice')
    User = apps.get_model('accounts', 'User')

    payment = Payment.objects.last()

    invoice = Invoice(
        client=payment.user),
        status=5
    )
    invoice.save()

Also tried

    invoice = Invoice(
        client=User.objects.get(pk=payment.user.pk),
        status=5
    )

In both cases I get the error:

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

Change History (1)

comment:1 by Odero, 8 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top