1. Curtis Maloney
  2. gnocchi-tools

Commits

funkybob  committed c02f1a3

Fixed defaults handling in AutoOneToOneField

  • Participants
  • Parent commits 6845ee9
  • Branches default

Comments (0)

Files changed (1)

File gnocchi/tools/fields.py

View file
  • Ignore whitespace
 from django.db.models.fields import related
 from django.db import router
 
-# XXX Add a way to provide a "build one for me" hook
 class AutoSingleRelatedObjectDescriptor(related.SingleRelatedObjectDescriptor):
-    def __init__(self, related, defaults):
+    def __init__(self, related, defaults=None):
         self._defaults = defaults or {}
         super(AutoSingleRelatedObjectDescriptor, self).__init__(related)
 
         except AttributeError:
             rel = self.related
             params = {'%s' % rel.field.name: instance}
-            defaults = self._defaults or {}
+            defaults = self._defaults
             if callable(defaults):
                 defaults = defaults(instance)
             params['defaults'] = self._defaults
     get_or_create.  It should be either a dict, or a callable that accepts one
     parameter: the instance following the relation.
     '''
-    def __init__(self, defaults=None, **kwargs):
-        self.defaults = defaults
-        super(AutoOneToOneField, self).__init__(self, **kwargs)
+    def __init__(self, *args, **kwargs):
+        super(AutoOneToOneField, self).__init__(*args, **kwargs)
+        self.defaults = kwargs.get('defaults', {})
 
     def contribute_to_related_class(self, cls, related):
         setattr(cls, related.get_accessor_name(),