Maybe using a Model like Kurtis mentioned will be better (and easier) than 
making a custom field... ?


Le jeudi 23 août 2012 18:49:55 UTC+2, Michael Palumbo a écrit :
>
> 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
>    1. 
>       
>                   else:
>       
>       2. 
>       
>                       attrs['id'] = self.html_initial_id
>       
>       3. 
>       
>       4. 
>       
>               if not only_initial:
>       
>       5. 
>       
>                   name = self.html_name
>       
>       6. 
>       
>               else:
>       
>       7. 
>       
>                   name = self.html_initial_name
>       
>       1. 
>       
>               return widget.render(name, self.value(), attrs=attrs)
>       
>       ...
>    1. 
>       
>       2. 
>       
>           def as_text(self, attrs=None, **kwargs):
>       
>       3. 
>       
>               """
>       
>       4. 
>       
>               Returns a string of HTML for representing this as an <input 
> type="text">.
>       
>       5. 
>       
>               """
>       
>       6. 
>       
>               return self.as_widget(TextInput(), attrs, **kwargs)
>       
>       ▼ Local vars <http://127.0.0.1:8000/admin/xmlmapping/mapping/3/#>
>    VariableValueauto_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
>    1. 
>       
>               """
>       
>       2. 
>       
>               Returns the value for this BoundField, using the initial value 
> if
>       
>       3. 
>       
>               the form is not bound or the data otherwise.
>       
>       4. 
>       
>               """
>       
>       5. 
>       
>               if not self.form.is_bound:
>       
>       6. 
>       
>                   data = self.form.initial.get(self.name, self.field.initial)
>       
>       7. 
>       
>                   if callable(data):
>       
>       1. 
>       
>                       data = data()
>       
>       ...
>    1. 
>       
>               else:
>       
>       2. 
>       
>                   data = self.field.bound_data(
>       
>       3. 
>       
>                       self.data, self.form.initial.get(self.name, 
> self.field.initial)
>       
>       4. 
>       
>                   )
>       
>       5. 
>       
>               return self.field.prepare_value(data)
>       
>       6. 
>       
>       ▼ Local vars <http://127.0.0.1:8000/admin/xmlmapping/mapping/3/#>
>    VariableValueself
>    
>    <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.
>
>
>
> Le jeudi 23 août 2012 03:27:09 UTC+2, Melvyn Sopacua a écrit :
>>
>> On 23-8-2012 0:37, Michael Palumbo wrote: 
>> > ok thanks, so you'd rather do it with a Model than a custom field. 
>> > I actually started to make a custom field but I had a few issues while 
>> > displaying it in the admin so that's why I asked the question then. 
>>
>> You can pickle an object and store the result in a blob-type field or 
>> encode it and put it in a TextArea field: 
>> <http://docs.python.org/library/pickle.html#pickle> 
>> It's basically what the session module is doing. Perhaps look there for 
>> inspiration: 
>> <
>> https://docs.djangoproject.com/en/1.4/topics/http/sessions/#technical-details>
>>  
>>
>>
>> -- 
>> Melvyn Sopacua 
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/NOxnCEnchLsJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to