I am using a custom Django model field to and widget to render a GoogleMap widget in my admin, i also want to use South with my project to handle database migrations. However after much effort i am unable to generate a custom South rule that fits, this are my custom model and the last of the many instrospection rules that i've tried.
class GoogleMapMarkerField(models.CharField):
__metaclass__ = models.SubfieldBase
description = _('Un marcador de Google Maps')
widget = GoogleMapMarkerWidget
def __init__(self, center, *args, **kwargs):
kwargs['max_length'] = 100
kwargs['help_text'] = _('Arrastre el cursor en el mapa para seleccionar el punto')
self.center = center
super(GoogleMapMarkerField, self).__init__(*args, **kwargs)
def formfield(self, **kwargs):
defaults = {
'center': self.center,
'form_class':GoogleMa开发者_如何转开发pMarkerFormField
}
defaults.update(kwargs)
return super(GoogleMapMarkerField, self).formfield(**defaults)
def to_python(self, value):
if isinstance(value, GoogleMapMarker):
return value
if isinstance(value, list):
return GoogleMapMarker(*map(float, value))
elif isinstance(value, basestring):
try:
return GoogleMapMarker(*map(float, value.split(',')))
except ValueError:
pass
def get_prep_value(self, value):
return '%f,%f' % (value.latitude, value.longitude)
add_introspection_rules([
(
(GoogleMapMarkerField, ),
[],
{
'center': ('center', {}),
}
)
], ["^website\.fields\.GoogleMapMarkerField"])
And this is the traceback that i'm getting
Traceback (most recent call last):
File "manage.py", line 14, in <module>
execute_manager(settings)
File "/home/armonge/workspace/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/home/armonge/workspace/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/armonge/workspace/env/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/armonge/workspace/env/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/home/armonge/workspace/env/lib/python2.7/site-packages/south/management/commands/schemamigration.py", line 97, in handle
old_orm = last_migration.orm(),
File "/home/armonge/workspace/env/lib/python2.7/site-packages/south/utils.py", line 62, in method
value = function(self)
File "/home/armonge/workspace/env/lib/python2.7/site-packages/south/migration/base.py", line 422, in orm
return FakeORM(self.migration_class(), self.app_label())
File "/home/armonge/workspace/env/lib/python2.7/site-packages/south/orm.py", line 46, in FakeORM
_orm_cache[args] = _FakeORM(*args)
File "/home/armonge/workspace/env/lib/python2.7/site-packages/south/orm.py", line 125, in __init__
self.models[name] = self.make_model(app_label, model_name, data)
File "/home/armonge/workspace/env/lib/python2.7/site-packages/south/orm.py", line 321, in make_model
field = self.eval_in_context(code, app, extra_imports)
File "/home/armonge/workspace/env/lib/python2.7/site-packages/south/orm.py", line 236, in eval_in_context
return eval(code, globals(), fake_locals)
File "<string>", line 1, in <module>
TypeError: __init__() takes at least 2 arguments (1 given)
center
isn't a keyword argument, it's a positional argument. You shouldn't use positional arguments with South, it doesn't understand them. (See Custom Fields: Keyword Arguments). You could solve this by providing center with a default value( center=None
would be fine) and then following the example code at the link for defining the keyword name as passed to __init__
, the name as stored in the database, and a dictionary of options (may be blank, but setting the default value there too helps).
精彩评论