ModelForms and admin.autodiscover
See original GitHub issueHello, 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:
- Created 9 years ago
- Comments:8
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Isn’t the problem the fact that you missed
'body_el'
infields
?If not:
admin.autodiscover
is rather weird way of solving this. All it does is importingmodeltranslation.admin
, where are some classed defined. And it importmodeltranslation.models
. Ensuremodeltranslation
is listed inINSTALLED_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 thatmodeltranslation
is executed at the beggining.Great to know! I hadn’t heard of
explicit setup
and it seems to be the solution for debug toolbar vs modeltranslation issues.