I did not know the pickle module. This is interesting. Thanks.However, after playing with it, I am not sure it fits totally my need because I'd like humans to be able to write a string like "modulea.moduleb.MyClass" in the admin or so.
So I kept trying making a custom field (instead of a Model) by creating a custom field, formfield and widget.
The list view works well, even though it displays <class 'modulea.moduleb.MyClass'> instead of modulea.moduleb.MyClass...
The add page works as well, I can add a model with the value "modulea.moduleb.MyClass" in the field and then it works.
BUT I have an issue with the detail page of a model.
I get a TypeError because my field has a class reference (which is callable) and the code below then instances it!
c:\Users\Mike\Dropbox\development\tools\virtualenvs\django-1.4\lib\site-packages\django\forms\forms.py
in as_widget
else:
attrs['id'] = self.html_initial_id
if not only_initial:
name = self.html_name
else:
name = self.html_initial_name
return widget.render(name, self.value(), attrs=attrs)
...
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
Variable | Value |
---|
auto_id | u'id_mapper_class' |
widget | <xmlmapping.fieldsbis.PickleWidget object at 0x05B13290> |
name | 'mapper_class' |
self | <django.forms.forms.BoundField object at 0x05B1A9F0> |
only_initial | False |
attrs | {'id': u'id_mapper_class'} |
c:\Users\Mike\Dropbox\development\tools\virtualenvs\django-1.4\lib\site-packages\django\forms\forms.py
in value
"""
Returns the value for this BoundField, using the initial value if
the form is not bound or the data otherwise.
"""
if not self.form.is_bound:
data = self.form.initial.get(self.name, self.field.initial)
if callable(data):
data = data()
...
else:
data = self.field.bound_data(
self.data, self.form.initial.get(self.name, self.field.initial)
)
return self.field.prepare_value(data)
Variable | Value |
---|
self | <django.forms.forms.BoundField object at 0x05B1A9F0> |
data | <class 'xmlmapping.mappers.MapperWithNoLog'> |
I overrided the render method of the widget but the errors comes from self.value() of the BoundField.
How can I fix that? override that?
Any other idea?
Thanks.