Created
September 7, 2010 20:42
-
-
Save chrischambers/569060 to your computer and use it in GitHub Desktop.
0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models import ForeignKey | |
from django.contrib.auth.models import User | |
import threadlocals | |
class UserField(ForeignKey): | |
""" UserField | |
By defaults, foreign key to User; null=True, blank=True | |
""" | |
def __init__(self, **kwargs): | |
kwargs.setdefault('to', User) | |
kwargs.setdefault('null', True) | |
kwargs.setdefault('blank', True) | |
ForeignKey.__init__(self, **kwargs) | |
class CreatorField(UserField): | |
""" CreatorField | |
By default, sets editable=False, default=threadlocals.get_current_user | |
""" | |
def __init__(self, **kwargs): | |
kwargs.setdefault('editable', False) | |
kwargs.setdefault('default', threadlocals.get_current_user) | |
UserField.__init__(self, **kwargs) | |
class EditorField(CreatorField): | |
""" EditorField | |
By default, sets editable=False, default=threadlocals.get_current_user | |
Sets value to get_current_user() on each save of the model. | |
""" | |
def __init__(self, **kwargs): | |
super(CreatorField, self).__init__(**kwargs) | |
def pre_save(self, model_instance, add): | |
value = threadlocals.get_current_user() | |
setattr(model_instance, self.name, value) | |
if value: | |
value = value.pk | |
setattr(model_instance, self.attname, value) | |
return value | |
try: | |
from south.modelsinspector import add_introspection_rules | |
except ImportError: | |
add_introspection_rules = False | |
if add_introspection_rules: | |
add_introspection_rules([], [r"^threaded_multihost\.fields\.(User|Creator|Editor)Field"]) | |
# Traceback: | |
Traceback (most recent call last): | |
File "./manage.py", line 34, in <module> | |
execute_manager(settings) | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager | |
utility.execute() | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute | |
self.fetch_command(subcommand).run_from_argv(self.argv) | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/django/core/management/base.py", line 191, in run_from_argv | |
self.execute(*args, **options.__dict__) | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/django/core/management/base.py", line 218, in execute | |
output = self.handle(*args, **options) | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/south/management/commands/schemamigration.py", line 97, in handle | |
old_orm = last_migration.orm(), | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/south/utils.py", line 62, in method | |
value = function(self) | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/south/migration/base.py", line 422, in orm | |
return FakeORM(self.migration_class(), self.app_label()) | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/south/orm.py", line 46, in FakeORM | |
_orm_cache[args] = _FakeORM(*args) | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/south/orm.py", line 125, in __init__ | |
self.models[name] = self.make_model(app_label, model_name, data) | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/south/orm.py", line 318, in make_model | |
field = self.eval_in_context(code, app, extra_imports) | |
File "/Users/ajax/.virtualenvs/satchmo/lib/python2.6/site-packages/south/orm.py", line 236, in eval_in_context | |
return eval(code, globals(), fake_locals) | |
File "<string>", line 1, in <module> | |
File "/Users/ajax/.virtualenvs/satchmo/src/django-threaded-multihost/threaded_multihost/fields.py", line 40, in __init__ | |
super(CreatorField, self).__init__(**kwargs) | |
File "/Users/ajax/.virtualenvs/satchmo/src/django-threaded-multihost/threaded_multihost/fields.py", line 16, in __init__ | |
ForeignKey.__init__(self, User, **kwargs) | |
TypeError: __init__() got multiple values for keyword argument 'to' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment