Skip to content

Instantly share code, notes, and snippets.

@jonashaag
Created September 7, 2011 09:41
Show Gist options
  • Save jonashaag/1200165 to your computer and use it in GitHub Desktop.
Save jonashaag/1200165 to your computer and use it in GitHub Desktop.

Revisions

  1. jonashaag revised this gist Sep 7, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 3-admin.py
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,4 @@ def categories(instance):
    class PostAdmin(ModelAdmin):
    list_display = ['title', categories]

    site.register(Post, PostAdmin)
    site.register(Post, PostAdmin)
  2. jonashaag revised this gist Sep 7, 2011. 4 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  3. jonashaag renamed this gist Sep 7, 2011. 1 changed file with 0 additions and 0 deletions.
  4. jonashaag revised this gist Sep 7, 2011. 3 changed files with 31 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions admin.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    from django.contrib.admin import site, ModelAdmin
    from models import Post

    def categories(instance):
    return ', '.join(instance.categories)

    class PostAdmin(ModelAdmin):
    list_display = ['title', categories]

    site.register(Post, PostAdmin)
    10 changes: 10 additions & 0 deletions forms.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    from django import forms

    class StringListField(forms.CharField):
    def prepare_value(self, value):
    return ', '.join(value)

    def to_python(self, value):
    if not value:
    return []
    return [item.strip() for item in value.split(',')]
    11 changes: 11 additions & 0 deletions models.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    from django.db import models
    from djangotoolbox.fields import ListField
    from .forms import StringListField

    class CategoryField(ListField):
    def formfield(self, **kwargs):
    return models.Field.formfield(self, StringListField, **kwargs)

    class Post(models.Model):
    title = models.CharField(max_length=100)
    categories = CategoryField()
  5. jonashaag revised this gist Sep 7, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion howto-listfield-django-admin.rst
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ Trying to edit some posts in the admin crashes with::
    Solution
    --------
    What we need to do is to teach the admin how to display a ``ListField`` in the
    edit view. Our form field will a simple ``<input type=text>`` input box with
    edit view. Our form field will be a simple ``<input type=text>`` input box with
    comma-separated category names. For more about custom form fields, refer to the
    `Django documentation <https://docs.djangoproject.com/en/dev/ref/forms/fields/#creating-custom-fields>`_
    and your favourite search engine using the terms "Django custom form field".
  6. jonashaag revised this gist Sep 7, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion howto-listfield-django-admin.rst
    Original file line number Diff line number Diff line change
    @@ -71,7 +71,7 @@ string which is then displayed in the input box.

    Let's add a post and check out the resulting model object in the database:

    .. image:: edit-form.png
    .. image:: http://img30.imageshack.us/img30/2391/editform.png

    ::

  7. jonashaag created this gist Sep 7, 2011.
    81 changes: 81 additions & 0 deletions howto-listfield-django-admin.rst
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    Howto use ListFields in Django's admin
    ======================================

    Problem
    -------
    Consider this blog post model:

    ``models.py``
    .............
    ::

    from django.db import models
    from djangotoolbox.fields import ListField

    class Post(models.Model):
    title = models.CharField(max_length=100)
    categories = ListField()


    ``admin.py``
    ............
    ::

    from django.contrib.admin import site
    from models import Post

    site.register(Post)


    Trying to edit some posts in the admin crashes with::

    No form field implemented for <class 'djangotoolbox.fields.ListField'>


    Solution
    --------
    What we need to do is to teach the admin how to display a ``ListField`` in the
    edit view. Our form field will a simple ``<input type=text>`` input box with
    comma-separated category names. For more about custom form fields, refer to the
    `Django documentation <https://docs.djangoproject.com/en/dev/ref/forms/fields/#creating-custom-fields>`_
    and your favourite search engine using the terms "Django custom form field".

    First, we need to subclass ``ListField`` to override the ``formfield`` method::

    from .forms import StringListField

    class CategoryField(ListField):
    def formfield(self, **kwargs):
    return models.Field.formfield(self, StringListField, **kwargs)

    class Post(models.Model):
    title = models.CharField(max_length=100)
    categories = CategoryField()

    Then, in ``forms.py``, we define ``StringListField``::

    from django import forms

    class StringListField(forms.CharField):
    def prepare_value(self, value):
    return ', '.join(value)

    def to_python(self, value):
    if not value:
    return []
    return [item.strip() for item in value.split(',')]

    This will covert the comma-separated input box contents into a Python ``list``,
    and the ``list`` value that is fetched from the database int a comma-separated
    string which is then displayed in the input box.

    Let's add a post and check out the resulting model object in the database:

    .. image:: edit-form.png

    ::

    >>> Post.objects.get(title='foo').categories
    [u'spam', u'eggs', u'bar']

    It worked! Simple, isn't it?