Created
July 12, 2015 15:31
-
-
Save maximiliano/fb8f1259a119b5da89eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FILE models.py | |
from django.contrib.auth.models import User | |
from django.db import models | |
class Profile(models.Model): | |
user = models.OneToOneField(User) | |
nome = models.CharField(u'nome', null=True, blank=True, max_length=80) | |
matricula = models.CharField(u'matricula', null=True, blank=True, max_length=80) | |
cargo = models.CharField(u'cargo', null=True, blank=True, max_length=80) | |
# FILE migrations/xxxx_foo.py | |
# -*- coding: utf-8 -*- | |
from __future__ import unicode_literals | |
from django.contrib.auth.models import User | |
# from django.contrib.auth import get_user_model | |
from django.db import migrations | |
def create_profiles(apps, schema_editor): | |
Profile = apps.get_model('core', 'Profile') | |
print User # <class 'django.contrib.auth.models.User'> | |
print Profile # <class 'Profile'> | |
for user in User.objects.all(): | |
profile = Profile(user=user) | |
profile.save() | |
class Migration(migrations.Migration): | |
dependencies = [ | |
('foo', 'xxxy_foo'), | |
] | |
operations = [ | |
migrations.RunPython(create_profiles), | |
] | |
# ERROR: | |
# File ".../migrations/0008_auto_20150712_1502.py", line 19, in create_profiles | |
# profile = Profile(user=user) | |
# File ".../lib/python2.7/site-packages/django/db/models/base.py", line 468, in __init__ | |
# setattr(self, field.name, rel_obj) | |
# File ".../lib/python2.7/site-packages/django/db/models/fields/related.py", line 627, in __set__ | |
# self.field.rel.to._meta.object_name, | |
# ValueError: Cannot assign "<User: admin>": "Profile.user" must be a "User" instance. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment