Update reset_polymorphic_ctype, improve ignore_existing parameter

fix_request_path_info
Diederik van der Boor 2017-01-09 15:32:52 +01:00
parent add90aac4f
commit cf663a0e07
1 changed files with 12 additions and 5 deletions

View File

@ -1,4 +1,5 @@
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db import DEFAULT_DB_ALIAS
def reset_polymorphic_ctype(*models, **filters): def reset_polymorphic_ctype(*models, **filters):
@ -10,12 +11,18 @@ def reset_polymorphic_ctype(*models, **filters):
Add ``preserve_existing=True`` to skip models which already Add ``preserve_existing=True`` to skip models which already
have a polymorphic content type. have a polymorphic content type.
""" """
preserve_existing = filters.pop('preserve_existing', False) using = filters.pop('using', DEFAULT_DB_ALIAS)
for new_model in models: ignore_existing = filters.pop('ignore_existing', False)
new_ct = ContentType.objects.get_for_model(new_model) if ignore_existing:
# When excluding models, make sure we don't ignore the models we
# just assigned the an content type to. hence, start with child first.
models = reversed(models)
qs = new_model.objects.all() for new_model in models:
if preserve_existing: new_ct = ContentType.objects.db_manager(using).get_for_model(new_model)
qs = new_model.objects.db_manager(using)
if ignore_existing:
qs = qs.filter(polymorphic_ctype__isnull=True) qs = qs.filter(polymorphic_ctype__isnull=True)
if filters: if filters:
qs = qs.filter(**filters) qs = qs.filter(**filters)