When you app depends on a custom user model

53 views
Skip to first unread message

bnmng

unread,
Oct 9, 2021, 12:01:39 PM10/9/21
to Django users
Django "strongly recommends" you create custom user, and in your apps you can refer to the custom user with "settings.AUTH_USER_MODEL" or with get_user_model().  

Here's the problem.  If you develop an app which refers to the custom user, the migrations won't be portable because even if you use the above references, the migration will use the app name and model name of the custom user.  

Here's what a migration might look like: 
 
dependencies = [
        ('my_custom_auth_app', '0001_initial'),
]

operations = [
        migrations.AddField(
            model_name='mymodel',
            name='created_by',
            field=models.ForeignKey(
                  on_delete=django.db.models.deletion.CASCADE,
                  to='my_custom_auth_app.my_custom_user',
            ),
        ),
 ]

Even though I never put "my_custom_auth_app" or "my_custom_user" in the app, it translated settings.AUTH_USER_MODEL to the app name and model for the migration.

Is there any way to get around this and make your app with its migrations portable?

Harouna Diallo

unread,
Oct 9, 2021, 4:40:32 PM10/9/21
to django...@googlegroups.com
I agree

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8cad467d-5fcc-4d22-9915-64290a1bc9a7n%40googlegroups.com.

David Nugent

unread,
Oct 10, 2021, 5:56:07 PM10/10/21
to django...@googlegroups.com
Why not just edit the migration file directly and substitute 'my_custom..' for settings.AUTH_USER_MODEL?

There should be no problem in doing that.

Regards, David

--

bnmng

unread,
Oct 11, 2021, 9:17:39 AM10/11/21
to Django users
That's genius, David.  Thank you!

bnmng

unread,
Oct 11, 2021, 10:47:49 AM10/11/21
to Django users
One more note... I deleted the database and migrations and started clean again, and this time when I opened the migration file it had setting.AUTH_USER_MODEL the way I wanted it.  So I don't know why it didn't do that before.
Reply all
Reply to author
Forward
0 new messages