问题
When I run tests I get this error during database initialization:
django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<ModelState: 'users.GroupProxy'>]
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
I created this proxy for contrib.auth Group model to place it in my app in django admin:
class GroupProxy(Group):
class Meta:
proxy = True
verbose_name = Group._meta.verbose_name
verbose_name_plural = Group._meta.verbose_name_plural
So what can I do to fix this issue?
回答1:
After a lot of digging on this the only thing that worked for me was
comment out the offending apps, run migrations, then add them in again.
Just a workaround but hopefully it helps somebody.
回答2:
I've come across this issue, and as commenting out the model isn't really a solution, I've found that setting the undocumented auto_created = True
to the Meta class will make Django ignore it.
class GroupProxy(Group):
class Meta:
proxy = True
auto_created = True
回答3:
Simply creating a migrations
directory at the root of your app (so users/migrations/
in your case) and adding an empty __init__.py
file might resolve your issue. At least it did for me when I was getting the same error.
But you're better off running makemigrations
for your app, as suggested by
@zenofewords above. That will create the directory for you AND generate migrations for your proxy models.
Why does Django create migration files for proxy models?
Your tests are looking for those migrations and aren't finding them.
回答4:
Have you tried running manage.py makemigrations <app_label>
on your app before running tests?
Also, check if the app which model(s) you are trying to Proxy is included in your INSTALLED_APPS.
回答5:
Having spent the majority of this afternoon trying to solve this error myself, going through every conceivable mixture of 'commenting out apps', 'dropping tables' and dropping entire databases I found that my issue was caused by a simple lack of a 'migrations' folder and an __ init__.py file inside of said folder.
One of the older answers which was correct is now no longer correct as they have fixed the issue mentioned here.
Check every directory that contains the model mentioned in 'init.py' and it should go away.
Probably won't solve everyone's case but it helped mine.
回答6:
I also faced this issue (after doing some complex model inheritance). One of my migrations contained
migrations.CreateModel(
name='Offer',
fields=[
# ...
],
options={
# ...
},
bases=('shop.entity',),
),
I deleted shop.Entity
model entirely, but the migration was referencing it in bases
attribute. So I just deleted bases=('shop.entity',)
and it works. It will probably break an opportunity to migrate from the beginning, but at least allows to migrate further on.
Another advice would be: go directly to django code and inspect what's causing "bases" trouble. Go to django/db/migrations/state.py
and add a breakpoint:
try:
bases = tuple(
(apps.get_model(base) if isinstance(base, six.string_types) else base)
for base in self.bases
)
except LookupError:
print(self.bases) # <-- print the bases
import ipdb; ipdb.set_trace() # <-- debug here
raise InvalidBasesError("Cannot resolve one or more bases from %r" % (self.bases,))
回答7:
I had this problem after I renamed the parent table of a bunch of my proxy models. I resolved it by:
- Deleting the migration file that had the name change for the parent table.
- Using the postgres terminal, I renamed the parent table to its previous name.
- Ran
makemigrations
andmigrate
again
回答8:
If this is only happening when running python manage.py test
(maybe because you have already made the necessary migrations), you should explicitly say that contrib.auth
should not migrate in the MIGRATION_MODULES
of your settings module.
MIGRATION_MODULES(
'auth': "contrib.auth.migrations_not_used_in_tests",
)
回答9:
One possibility is that the deleting or creating of a model in the migration file is in the wrong order. I experienced this in Django 1.7.8 when the base model was BEFORE the derived model. Swapping the order for deleting the model fixed the issue.
回答10:
happened to me with no other app - just because I renamed a model which was a base for other models (and maybe created that sub-models within the same migration) renaming the super-model to it's original name solved it for me
回答11:
I faced the same issue and adding app_label
attribute in class Meta:
solved the error :
class GroupProxy(Group):
class Meta:
proxy = True
app_label = '<app in which you want this model>'
verbose_name = Group._meta.verbose_name
verbose_name_plural = Group._meta.verbose_name_plural
来源:https://stackoverflow.com/questions/30267237/invalidbaseserror-cannot-resolve-bases-for-modelstate-users-groupproxy