Commits

João Pedro Francese committed 8d28ca6

Renamed models.utils.clone_model_instance() to clone_instance().
Added SelectRelatedManager.

  • Participants
  • Parent commits af6166f

Comments (0)

Files changed (2)

File inoa/models/managers.py

+# -*- coding: utf-8 -*-
+
+from django.db import models
+
+class SelectRelatedManager(models.Manager):
+    """
+    A Manager whose querysets always include select_related() by default.
+    Parameters passed in the constructor will be used directly in select_related.
+    """
+    def get_query_set(self):
+        return super(SelectRelatedManager, self).get_query_set().select_related(*self.args, **self.kwargs)
+    
+    def __init__(self, *args, **kwargs):
+        super(SelectRelatedManager, self).__init__()
+        self.args = args
+        self.kwargs = kwargs

File inoa/models/utils.py

         return u"%s #%d" % (obj._meta.verbose_name.capitalize(), obj.id)
 setattr(models.Model, '__unicode__', default_repr)
 
-def clone_model_instance(obj):
-    """Returns an instance (non-saved) of obj's model with the same attributes as obj."""
+def clone_instance(obj):
+    """
+    Returns a new instance of obj's model, with the same attributes as obj.
+    The new instance will not have been saved, and all primary keys will be cleared.
+    """
     # Source: http://djangosnippets.org/snippets/904/ (modified with the first comment's suggestion)
     attributes = []
     for field in obj._meta.fields: