1. Michael Elsdörfer
  2. djutils


Michael Elsdörfer  committed a71a30c Merge

Merged upstream.

  • Participants
  • Parent commits 5f6bed1, 12ff560
  • Branches djutils

Comments (0)

Files changed (2)

File djutils/features/listfield/fields.py

View file
  • Ignore whitespace
         # unserialize strings to a list
         return PrettyList(string_to_list(value))
-    def get_db_prep_save(self, value):
+    def get_db_prep_save(self, value, connection):
         # serialize lists to a string
         if isinstance(value, list):
             return list_to_string(value)

File djutils/models/__init__.py

View file
  • Ignore whitespace
     return obj.__class__(**initial)
+from django.db.models.deletion import Collector
+from django.db import router
 def has_related_objects(obj, ignore=[]):
     """Returns ``True`` if the model instance ``obj`` has any related
     objects depending on it.
     # TODO: untested with multi-level inheritance, multiple parents
     # TODO: add tests
+    model = type(obj)
-    related = obj._meta.get_all_related_objects()
-    model = type(obj)
-    for so_klass in related:
+    collector = Collector(using=router.db_for_write(model))
+    collector.collect([obj])
+    for so_klass, items in collector.data.items():
+        # Make sure there are instances for this relation.
+        if not len(items):
+            continue
         # Auto-created m2m models we don't care about. The admin ignores
         # those in it's ``django.contrib.admin.util.NestedObjects`` class.
         if so_klass._meta.auto_created:
-        # Also ignore any subclasses.
-        if issubclass(model, so_klass):
+        # Also ignore any subclasses (in case of inheritance).
+        if issubclass(so_klass, model):
         # Does the user explicitely ask us to ignore this model?
         if so_klass in ignore: