#29283: Difference between 'apps.get_model' and 'import' in migration
-------------------------------------+-------------------------------------
               Reporter:  bronvic    |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  1.11
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 HTR:

 I have a model `UserProfile` which looks like this:
 {{{
 class UserProfile(VStandardModel):
     user = models.OneToOneField(settings.AUTH_USER_MODEL,
 on_delete=models.CASCADE, null=True, default=None)
     mail = models.TextField(null=True, default=None, unique=True)
 }}}

 And I want to make migration which will do two things:
 1. Change user field to `user =
 models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
 null=False)`
 2. If user was `None` set a user with an email

 So I wrote a migration:
 {{{
 from __future__ import unicode_literals

 from django.conf import settings
 from django.db import migrations, models
 from django.core.exceptions import ObjectDoesNotExist
 from django.contrib.auth import get_user_model

 import django.db.models.deletion


 def empty_users(apps, schema_editor):
     UserProfile = apps.get_model('main', 'UserProfile')
     model_user = get_user_model()

     for profile in UserProfile.objects.all().only('user', 'mail'):
         if profile.user is None:
             try:
                 profile.user = model_user.objects.get(email=profile.mail)
             except model_user.DoesNotExist:
                 try:
                     profile.user =
 model_user.objects.create(email=profile.mail, is_active=False)
                 except Exception as err:
                     print(err)

             profile.save()


 class Migration(migrations.Migration):

     dependencies = [
         ('main', '0066_auto_20180222_1954'),
     ]

     operations = [
         migrations.RunPython(empty_users,
 reverse_code=migrations.RunPython.noop),
         migrations.AlterField(
             model_name='userprofile',
             name='user',
 field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE,
 to=settings.AUTH_USER_MODEL),
         ),
     ]
 }}}

 And, as you see, I emphasized an error in `except Exception as err:`. It
 is: `Cannot assign "<User: >": "UserProfile.user" must be a "User"
 instance.`.

 But if I change `UserProfile = apps.get_model('main', 'UserProfile')` to
 `from main.models import UserProfile` migration will pass.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29283>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.745ae8e878393a1d6ad0cda13f763958%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to