Django Migrations and custom user models (admin.LogEntry.user lookup failed)
As I’m working with the Django 1.7 RC in one of my projects, I sometimes get unexpected (by me) behaviour. I just passed one of them. I start working with the default user model, but after that I realize that I need to change it in order to include some fields. I follow this steps as I just wanted to extend the model.
Lookup failed for model referenced by field admin.LogEntry.user
I created a model extending AbstractUser, added some fields, set AUTH_USER_MODELS setting according, created a new migration, and then apply the migrations, but then I get this error
ValueError: Lookup failed for model referenced by field admin.LogEntry.user: <app.Model>
After some googling I found this ticket.
The reasons for this is the use of “swappable_dependecy” that just “Turns a setting value into a dependency.”. This is used because AUTH_USER_MODEL is a settings that holds the actual model referenced by LogEntry. swappable_dependency hardcodes the __first__ migration, so if you don’t define your custom model in the first migration you will get this error.
I could use “run_before” (which at this time is undocumented) to bypass this behaviour, I don’t know whether or not is a good idea to bypass expected and documented behavoiur using an undocumented feature (I guess it’s not a good idea
) but, as this is not critical to me at this time, I’m going to let it go, and maybe redo my migrations when needed before deploying it.
run_before = [
('admin', '__first__'),
]