开发者

PlaceholderAdmin throws <lambda>() takes exactly 1 argument (2 given)

开发者 https://www.devze.com 2023-02-17 17:15 出处:网络
I\'ve wrote a plugin for django-cms which has it\'s own model with one PlaceholderField. When I add a PlaceholderAdmin for model admin I\'m getting this on admin site:

I've wrote a plugin for django-cms which has it's own model with one PlaceholderField. When I add a PlaceholderAdmin for model admin I'm getting this on admin site:

Exception Type: TemplateSyntaxError
Exception Value:    
Caught TypeError while rendering: <lambda>() takes exactly 1 argument (2 given)
Exception Location: <blablapath>/python2.6/site-packages/cms/forms/widgets.py in render, li开发者_开发技巧ne 199

I've been searching for solution and found only some problems with django-cms example which would not run without uncommenting some path in urls.py so I guess it might be problem with urls, especially that I do some magic in my urls. The question is: what conditions should hold for django-cms url's to be valid? Any ideas? Any solutions? Anybody had this problem before?


This issue is caused when you are not subclassing the PlaceholderAdminField in your admin class.

For example:

from cms.admin.placeholderadmin import PlaceholderAdmin
from cms.models.fields import PlaceholderField

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    sidebar = PlaceholderField('sidebar')

class MyAdmin(PlaceholderAdmin):
    """ Put your usual admin stuff here. If you use fieldset,
    include the sidebar as its own tuple """
    fieldsets = (
        (None, {
            'fields': ('name',),
        }),

        ('Sidebar', {
            'classes': ('plugin-holder', 'plugin-holder-nopage'),
            'fields': ('sidebar',)
        }),
    )
admin.site.register(MyModel, MyAdmin)


Well, this appears to be a bug in Django-CMS. I've never used it, but looking at the code, it's clear that the default formfield for PlaceholderField creates a lambda which takes a single argument - a queryset, but the render function for that formfield calls the lambda with two arguments - the request, and the queryset.

Probably the best way to fix this is to define your own subclass of PlaceholderField which redefines the formfield method so it returns a correct lambda:

class FixedPlaceholderField(PlaceholderField):
    def formfield(self, **kwargs):
        return self.formfield_for_admin(None, lambda request, qs: qs, **kwargs)

and use this field in your model instead.

If this fixes it, it's probably worth raising a bug in Django-CMS.


Ok. I've solved the problem for me. I still don't know what was happening but maybe my answer will help you figure it out ;). I've used a method:

def formfield_for_dbfield(self, db_field, **kwargs):
    ...

which substituted one text field into TinyMCE editor. When I removed the whole method the problem vanished. If I have time later on maybe I'll try to dig into it a little bit deeper. I'm still not convinced that that was the only problem. I have a hunch that it also might be something with data because I've filled placeholder with text plugin from code.

Hope it helps somehow.

0

精彩评论

暂无评论...
验证码 换一张
取 消