<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Django migration - Spheron1</title>
	<atom:link href="https://www.spheron1.uk/tag/django-migration/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.spheron1.uk</link>
	<description></description>
	<lastBuildDate>Sun, 15 May 2016 17:04:12 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>
	<item>
		<title>ValueError in Django migration</title>
		<link>https://www.spheron1.uk/2016/05/15/valueerror-in-django-migration/</link>
					<comments>https://www.spheron1.uk/2016/05/15/valueerror-in-django-migration/#respond</comments>
		
		<dc:creator><![CDATA[digitaldisaster]]></dc:creator>
		<pubDate>Sun, 15 May 2016 17:01:54 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Django migration]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://www.spheron1.uk/?p=845</guid>

					<description><![CDATA[<p>I&#8217;ve recently started developing in Python+Django again for a personal site that I&#8217;m working on and for far too long today I&#8217;ve been pulling my hair out when trying to write what should be a fairly simple migration using migrations.RunPython() to generate a default value to assign to a new OneToOneField column. The rather confusing error message that I was&#46;&#46;&#46;</p>
<p>The post <a href="https://www.spheron1.uk/2016/05/15/valueerror-in-django-migration/">ValueError in Django migration</a> first appeared on <a href="https://www.spheron1.uk">Spheron1</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>I&#8217;ve recently started developing in Python+Django again for a personal site that I&#8217;m working on and for far too long today I&#8217;ve been pulling my hair out when trying to write what should be a fairly simple migration using <em>migrations.RunPython()</em> to generate a default value to assign to a new OneToOneField column.</p>
<p>The rather confusing error message that I was receiving is:</p>
<p><code>ValueError: Cannot assign "&lt;User: blah&gt;": "Profile.account" must be a "User" instance.</code></p>
<p>This seems to be the same problem described at <a href="http://stackoverflow.com/questions/29700001/valueerror-cannot-assign-user-issue-on-my-onetoonefield-relationship">http://stackoverflow.com/questions/29700001/valueerror-cannot-assign-user-issue-on-my-onetoonefield-relationship</a>, but nothing mentioned in that post worked.</p>
<p>I was hopeful when I came across <a href="https://code.djangoproject.com/ticket/24282">https://code.djangoproject.com/ticket/24282</a> that this was a known issue, but alas whilst similar it&#8217;s not exactly the same problem and that bug was closed over a year ago. Back to square one!</p>
<p>What was particularly confusing was that the same code seemed to work as expected when run in the Django shell:</p>
<p><code>from foo.models import Profile, Article<br />
from django.contrib.auth.models import User<br />
first_superuser = User.objects.all().filter(is_superuser=True).first()<br />
default_profile = Profile(account=first_superuser, biography="")<br />
default_profile.save()</code></p>
<p>After a lot of fiddling, I eventually realised that the way which I was importing the User model from the built in Django auth system was wrong for a migration and so I replaced:</p>
<p><code>from django.contrib.auth.models import User</code></p>
<p>with:</p>
<p><code>User = apps.get_model('auth', 'user')</code></p>
<p>And now the migration finally works as expected. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p><p>The post <a href="https://www.spheron1.uk/2016/05/15/valueerror-in-django-migration/">ValueError in Django migration</a> first appeared on <a href="https://www.spheron1.uk">Spheron1</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.spheron1.uk/2016/05/15/valueerror-in-django-migration/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Object Caching 0/58 objects using Redis
Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Redis

Served from: spheron1.uk @ 2026-11-12 06:00:52 by W3 Total Cache
-->