question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModelForms and admin.autodiscover

See original GitHub issue

Hello, I am using Modeltranslation in a custom project which involves a custom admin backend (not django default), the site has 2 languages and since I use django crispy forms I opted for a solution for custom tabbed forms one for the english entries (default fields, as they update the _en fields) and one tab for the translated fields. Initially I had my form declared in forms.py the following way:

class ZadminPagesCreateForm(forms.ModelForm):
    class Meta:
        model = PagesContent
        fields = ['title','title_el','slug', 'categories',
        'status', 'flag_display_onmenu', 'body',
        'featured_image', 'enable_seo',
        'seo_title','seo_keywords','seo_description']

    def __init__(self, *args, **kwargs):
        super(ZadminPagesCreateForm, self).__init__(*args, **kwargs)
        print self.fields
        self.helper = FormHelper(self)
        self.helper.layout = Layout(
            TabHolder(
                Tab(_('English'),
                    Field('title', placeholder='title', css_class='sluggable'),
                    Field('slug', readonly='readonly', placeholder='slug is auto created'),
                    StrictButton(_('Edit Slug'), name="edit_slug", value="edit_slug", css_id="edit_slug", css_class="btn btn-default"),
                    Field('categories', css_class='input-sm zen_select'),
                    Field('status', css_class='input-sm'),
                    Field('flag_display_onmenu',),
                    Field('body', id="texteditor"),
                    'featured_image',
                    Field('enable_seo', css_class='toggle-seo'),
                    Fieldset(
                        _('SEO Options'),
                        Field('seo_title'),
                        Field('seo_keywords'),
                        Field('seo_description'),
                        css_id='seo_fieldset',
                    ),
                    FormActions(
                        Submit('save', _('Save')),
                        Button('cancel', _('Cancel'))
                    ),
                ),
                Tab(_('Greek'),
                    Field('title_el', placeholder=_('Greek Title')),
                    Field('body_el', id="texteditor"),
                ),
            )
        )

But I experienced problems:

django.core.exceptions.FieldError: Unknown field(s) (title_el) specified for PagesContent

I managed to find out that I need admin.autodiscover in my urls.py in order for the translation fields to appear. Is there a way around this?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
zlorfcommented, May 28, 2014

Isn’t the problem the fact that you missed 'body_el' in fields?

If not: admin.autodiscover is rather weird way of solving this. All it does is importing modeltranslation.admin, where are some classed defined. And it import modeltranslation.models. Ensure modeltranslation is listed in INSTALLED_APPS and if yes, try to change its order.

Also, see #243 (looks similar) - if ModelForm import model before translation registration, it will see old model version. You need to ensure that modeltranslation is executed at the beggining.

0reactions
zlorfcommented, May 29, 2014

Great to know! I hadn’t heard of explicit setup and it seems to be the solution for debug toolbar vs modeltranslation issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Django admin site
One of the most powerful parts of Django is the automatic admin interface. ... This class works like AdminConfig , except it doesn't...
Read more >
raw_id_fields for modelforms - django - Stack Overflow
You have to admin.autodiscover() to make this work. I'm writing this into an answer because there's a bit more missing. – chriscauley.
Read more >
Enable an autocomplete in admin forms in two steps
Make the admin Order form that uses PersonAutocomplete , in your_app/admin.py : ... There are two equivalent ways of doing this, using a...
Read more >
[Solved]-What does admin.autodiscover actually do?-django
admin.py is executed whenever your django loads URLconf from urls.py, the autodiscover() will search for all the apps in INSTALLED_APPS one by one...
Read more >
how the Django admin works - Ola Sitarska - Reinout van Rees
Autodiscover finds all your models and extracts information about them. Including the admin.py customization/configuration. This ends up as a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found