How do I extend django zinnia weblog entry

1.2k views Asked by At

I followed the instruction found here

http://django-blog-zinnia.readthedocs.org/en/latest/how-to/extending_entry_model.html

Unfortunately I am having trouble extending. First strange thing is that - I have to change the way the entry modules are imported:

from

from zinnia.models.entry import Entry
from zinnia.admin.entry import EntryAdmin

to

from zinnia.models import Entry
from zinnia.admin import EntryAdmin

After changing, I then ran the server and went to the admin page but then I got this error.

'RatingAdmin.fieldsets[0][1]['fields']' refers to field 'rating' that is missing from the form.

This is my code admin.py

from django.contrib import admin
from django.utils.translation import ugettext_lazy as _


from zinnia.models import Entry
from zinnia.admin import EntryAdmin

class RatingAdmin(EntryAdmin):
# into the 'Content' fieldset
fieldsets = ((_('Content'), {'fields': (
'title', 'content', 'image', 'status', 'rating')}),) + \
EntryAdmin.fieldsets[1:]

# Unregister the default EntryAdmin
# then register the EntryGalleryAdmin class
admin.site.unregister(Entry)
admin.site.register(Entry, RatingAdmin)

Here is my Abstract class model happy_models.py

from django.db import models
from zinnia.models.entry import EntryAbstractClass

class Happy(models.Model):
    rating = models.CharField(max_length=200)

    def __unicode__(self):
        return u'Rating %s' % self.title

    class Meta(EntryAbstractClass.Meta):
        abstract = True

Here is my zinnia entry base model path in settings.py

ZINNIA_ENTRY_BASE_MODEL = 'happy.happy_models.Happy'

I just noticed this at my console

/zinnia/models.py:30‌​2: RuntimeWarning: happy.happy_models.Happy cannot be imported

Here is my directory setup

happy/
    admin.py
    happy_models.py
    views.py

What I might be doing wrong when extending entry?

2

There are 2 answers

0
Michael Tsai On BEST ANSWER

I finally found out -- by running the server on interactive mode I was able to debug. The sample code had to be modified to this:-

from zinnia.models import EntryAbstractClass

instead of

from zinnia.models.entry import EntryAbstractClass
3
powellc On

I think you skipped over two parts:

  1. Create your own model that extends: EntryAbstractClass from zinnia.models.entry
  2. Tell zinnia to use your new model using the ZINNIA_ENTRY_BASE_MODEL setting in your settings.py file

Pay careful attention to how your structure your app extension as well, I usually use something like this:

/zinna_extras
    __init__.py
    entry_plus.py  <---- This is where your model that extends EntryAbstractClass goes
    admin.py <--- You got this part right above
    views.py <--- blank
    migrations/ <--- all original zinnia migrations + what you need for your model (see below)

Note, that there is NO models.py file. That screws up Zinnia's import style.

If you use South migrations, you also may want to set the following in settings.py:

SOUTH_MIGRATION_MODULES = {
    'zinnia': 'zinnia_extras.migrations.zinnia',
}

Copy all the original zinnia migrations into the migrations folder in your zinnia_extra app and then create any new migrations you need for your model. That just tells South to use your app as it's source for Zinnia migrations instead of the usual zinnia app.