gintas / django-picklefield

A pickled object field for Django
MIT License
180 stars 47 forks source link

how can I see the PickledObjectField in the admin #6

Closed alphydan closed 9 years ago

alphydan commented 11 years ago

First of all thank you for sharing this code. I think I've taken the appropriate steps but I can't see the field in the admin. Should I be able to see it and edit it?

models.py

from picklefield.fields import PickledObjectField # to store the curve
class Whateva(models.Model):
    pickled_curve =  PickledObjectField(_('The Curve y vs x'),
                                                             help_text=_('Insert the curve in the following format ((0,0),(1,0),(2,1),..etc,)'),)

admin.py

from django.contrib import admin
from myapp.models import Whateva

admin.site.register(Whateva)

Any tips appreciated, thank you.

ghinch commented 11 years ago

My solution for this was to create a custom form and use it in the admin. It inherits from forms.ModelForm like so:

class MyModelAdminForm(forms.ModelForm):
  picked_field = forms.CharField(widget=forms.Textarea())

  class Meta:
    model = MyModel
    fields = ['pickled_field', 'other_field',...]

  def __init__(self, *args, **kwargs):
    super(MyModelAdminForm, self).__init__(*args, **kwargs)
    self.fields['picked_field'].initial = self.instance.pickled_field
charettes commented 9 years ago

The field is automatically marked as editable=False hence why it's not displayed in the admin.

Since the HTML form representation of data stored in a PickledObjectField is highly application dependent and shouldn't be exposed to the end user in most cases I think we shouldn't ship a form field and keep things as they are.