django-fluent / django-fluent-contents

A widget engine to display various content on Django pages
http://django-fluent.org/
Apache License 2.0
149 stars 39 forks source link

Fluent content + parler integration issue #73

Closed sunjoomoon closed 7 years ago

sunjoomoon commented 8 years ago

Trying both fluent content and parler, following the doc from both repos returns below.

FieldError at /en/admin/fcontent/article/add/

Unknown field(s) (content) specified for Article. Check fields/fieldsets/exclude attributes of class ArticleAdmin.

More details

 .../lib/python2.7/site-packages/parler/admin.py in get_form
            return obj
        def get_form(self, request, obj=None, **kwargs):
            """
            Pass the current language to the form.
            """
                        form_class = super(TranslatableAdmin, self).get_form(request, obj, **kwargs)     ...

            if self._has_translatable_model():
                form_class.language_code = self.get_form_language(request, obj)
            return form_class

        def get_urls(self):

▶ Local vars
.../fluent_contents/admin/placeholderfield.py in get_form
        """
        placeholder_inline = PlaceholderFieldInline
        def get_form(self, request, obj=None, **kwargs):
            kwargs['formfield_callback'] = partial(
                self.formfield_for_dbfield, request=request, obj=obj)
            return super(PlaceholderFieldAdmin, self).get_form(
                            request, obj=obj, **kwargs)     ...

        def formfield_for_dbfield(self, db_field, **kwargs):
            obj = kwargs.pop('obj', None)
            if isinstance(db_field, PlaceholderField):
                kwargs['parent_object'] = obj
            return super(PlaceholderFieldAdmin, self).formfield_for_dbfield(

Seems can not find the field, thus throwing out above. Any work around?

vdboor commented 8 years ago

Do you have simple code examples that trigger this? We're using parler + fluent-contents all the time, and it works just fine in your case..

sunjoomoon commented 8 years ago

Let me try to reproduce the issue with another sandbox in due time.

sunjoomoon commented 7 years ago

Sorry for the long one, Actually I am trying out 3 combinations - parler + contents + mptt. On a fresh reinstallation (entier project, clean DB), the same is produced. It simpley can not find the 'content' field.

FYR, mptt + contents, without using Parler, is working fine without any issues though.

models.py

class Page(MPTTModel, TranslatableModel):
    parent = models.ForeignKey('self', null=True, blank=True,related_name='children')        
    translations = TranslatedFields(
        title = models.CharField(_('Title'), max_length=255, unique=True),
        slug = models.CharField(_('URL'), max_length=255,),
        content = PlaceholderField("article_content"),

        meta={
            'unique_together': (
                ('slug', 'language_code'),
            ),
        }          
    )
...

and in admins.py

from django.contrib import admin     
...
from parler.admin import TranslatableAdmin, TranslatableStackedInline, TranslatableTabularInline   
from parler.forms import TranslatableModelForm, TranslatedField   
...
from mptt.admin import DraggableMPTTAdmin, MPTTModelAdmin  
from mptt.forms import MPTTAdminForm
...
from fluent_contents.admin import PlaceholderFieldAdmin, PlaceholderEditorAdmin
...

class PageAdminForm(MPTTAdminForm, TranslatableModelForm):
    pass

class PageAdmin(PlaceholderFieldAdmin, MPTTModelAdmin, TranslatableAdmin):
    form = PageAdminForm

    list_display = ('title',)
    mptt_indent_field = "title"
    mptt_level_indent = 20    

    fieldsets = (
        (None, {
            'fields': ('parent','title', 'slug','content',),
        }),
    )

    def get_prepopulated_fields(self, request, obj=None):
        return {'slug': ('title',)}

admin.site.register(Page,PageAdmin)
vdboor commented 7 years ago

Ah, you don't have to place the PlaceholderField in the translated fields block. It can be assigned to the shared model, as it will detect it's a TranslatableModel.

sunjoomoon commented 7 years ago

Thanks, I did put that outside of the translations as below.

...
from fluent_contents.models.fields import PlaceholderField, PlaceholderRelation, ContentItemRelation
from fluent_contents.models import ContentItem

class Page(MPTTModel, TranslatableModel):
    parent = models.ForeignKey('self', null=True, blank=True,related_name='children')        
    translations = TranslatedFields(
        title = models.CharField(_('Title'), max_length=255, unique=True),
        slug = models.CharField(_('URL'), max_length=255,),

        meta={
            'unique_together': (
                ('slug', 'language_code'),
            ),
        }          
    )
content = PlaceholderField("article_content")
...

and now I get below issues when accessing the page in admin u"Key 'content' not found in 'PageForm'"

and in the browser,

Exception Type:     ValueError
Exception Value:    The field 'content' does not refer to a placeholder object!

in template file, something like below

{% extends "base.html" %}
{% load i18n mptt_tags static staticfiles %}
{% load fluent_contents_tags %}

{% block page_title %} {{ object.title }}{% endblock %}

{% block content %}

<div class="detailview">
{% render_placeholder content %}
</div>

{% endblock %}

Trying out {% render_placeholder content %} or {% render_placeholder object.content %} or {% render_placeholder page.content %} produce the same issue. Page is the app name.

what should I look into?

sunjoomoon commented 7 years ago

hmm.. I just tried something little, changed the content to another one, fluent_content, and then magically things just worked as expected. Guess something to do with fieldname content which I did something in the first place and then that kept somewhere and got me the issue. Thanks for the attention. Finally got mptt+Parler+Fluent-Contents working, I think.