Search code examples
pythondjangodjango-formsdjango-admindjango-parler

Creating Custom Forms in the Django Admin Interface with Parler


I have a django 1.8 instance (and python 2) and I am using django-parler for translation. I want to customize the admin interface (I want to use django-autocomplete-light, but that's not relevant). But customizing the admin interface with parler seems a bit more tricky then I thought. Here is a reduced example.

models.py

from django.db import models
from parler.models import TranslatableModel, TranslatedFields


class Fruits(TranslatableModel):
    translations = TranslatedFields(
        fname=models.CharField(max_length=200)
    )

    def __unicode__(self):
        return self.fname

forms.py

from dal import autocomplete
from django import forms

from .models import Fruits

class FruitsForm(forms.ModelForm):
    class Meta:
        model = Fruits
        fields = (
            'fruits',
        )

admin.py

from django.contrib import admin
from parler.admin import TranslatableAdmin

from .forms import FruitsForm
from .models import Fruits


class FruitsAdmin(TranslatableAdmin):
    form = FruitsForm
    model = Fruits


admin.site.register(Fruits, FruitsAdmin)

The Problem

django.core.exceptions.FieldError: Unknown field(s) (fruits) specified for Fruits

What can I do?


Solution

  • EDIT #1:

    After a bit of searching around, I believe that you have to make your form inherit from parler.formsTranslatableModelForm in order to work as expected.

    Change your FruitsForm to match the following:

    from dal import autocomplete
    from django import forms
    from parler.forms import TranslatableModelForm
    
    from .models import Fruits
    
    class FruitsForm(TranslatableModelForm):
        class Meta:
            model = Fruits
            fields = (
                'fname',
            )
    

    In case of fname giving you the same issue, try to set fields='__all__'

    Good luck :)