Domanda

I'm building an app for my Django 1.5.1 and Django-cms installation. The app meant to permit to upload an image linked to an URL. My code :

cms_plugins.py

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from cms.plugins.text.widgets.wymeditor_widget import WYMEditor
from django.forms.fields import CharField
from models import ImageText

class ImageTextPlugin(CMSPluginBase):
    model = ImageText
    name = "video wall"
    render_template = "video.html"

def render(self, context, instance, placeholder):
    context.update({
        'object':instance,
    })
    return context


  plugin_pool.register_plugin(ImageTextPlugin)

models.py

 from django.db import models
 from django.db.models import fields 
 from filer.fields.image import FilerImageField
 from filer.fields.file import FilerFileField
 from cms.models import CMSPlugin


 class ImageText(CMSPlugin):
      image = FilerImageField(related_name="image0")
      link = models.CharField(max_length=255, blank=True)

admin.py

 from django.contrib import admin
 from models import ImageText

 class ImageTextAdmin(admin.ModelAdmin):
    pass


  admin.site.register(ImageText, ImageTextAdmin)

video.html

 <a class="fancybox fancybox.iframe" rel="group" href="{{ instance.link }}">
   <div class="video">
        <img alt="work module image" src="{{ instance.image }}" />
        </div>
 </a>

When I try to install an instance of my app in a cms placeholder I have this error:

 ValueError at /admin/cms/page/51/edit-plugin/1839/
 Cannot assign "''": "ImageText.link" must be a "Link" instance.

I don't understand what causing that. Any idea?

my pip freeze

BeautifulSoup==3.2.1
Django==1.5.1
MySQL-python==1.2.4
PIL==1.1.7
Pillow==2.2.1
South==0.8.2
cmsplugin-filer==0.9.5
cmsplugin-zinnia==0.4
django-appconf==0.6
django-blog-zinnia==0.12.3
django-classy-tags==0.4
django-cms==2.4.2
django-filer==0.9.5
django-haystack==1.2.7
django-mptt==0.5.2 
django-polymorphic==0.5.3
django-sekizai==0.7
django-tagging==0.3.1
django-tinymce==1.5.1
django-twitter-tag==1.2
django-xmlrpc==0.1.5
easy-thumbnails==1.4
html5lib==1.0b3
pyparsing==1.5.7
pysolr==3.1.0
pytz==2013.7
requests==2.0.1
six==1.2.0
twitter==1.9.1
wsgiref==0.1.2
È stato utile?

Soluzione

The problem here has not much to do with your schema specifically but with how django handles OneToOne relationships.

When you subclass CMSPlugin, django creates an implicit OneToOne relationship from your model to the CMSPlugin and vice versa.

This being the case, imagine the following scenario:

I have a plugin called Title:

class Title(CMSPlugin):
    text = models.CharField(max_length=200)

the model above would have access to the CMSPlugin table via a cmsplugin_ptr attribute and a CMSPlugin instance would have access to it's Title via the automatically created attribute "title" <-- classname.lower()

So given the example above, if now I want another plugin called Video

class VideoPlugin(CMSPlugin):
    title = models.CharField(max_length=200)

here's the problem, when you save the video plugin, the title field clashes with the automatically generated title attribute from the previous plugin thus throwing a Cannot assign "''": "VideoPlugin.title" must be a "Title" instance.

This is mentioned very lightly in the docs.

So all you have to do in your case is rename the link field to something else. I usually name the fields of my plugins with their classname, so in your case I would end up with

class ImageText(CMSPlugin):
      image_text_image = FilerImageField(related_name="image0")
      image_text_link = models.CharField(max_length=255, blank=True)

this way you are sure to avoid any clashes with other plugins.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top