djangophotologue

Django-photologue ImageModel


This is my first project in django and im using photologue for gallery, which is awesome and i really like it.

But there is one thing i dont get, how can i use its ImageModel?

I have a blog-app and with every new blogpost created in the admin interface i woud like to upload a image which is linked to that post.

from django.db import models
from tagging.fields import TagField
from tinymce import models as tinymce_models
from photologue.models import ImageModel
#import datetime

# Create your models here.
class Blog(models.Model):
    title = models.CharField(max_length=150)
    content = tinymce_models.HTMLField()
    pub_date = models.DateTimeField(auto_now_add=True)
    edit_date = models.DateTimeField(auto_now=True)
    tags = TagField()
    summary = models.CharField(max_length=30)
    thumbnail = ImageModel()


def __unicode__(self):
    return self.title

This code above doesn't seem to work or do anything in fact.

I have been rifling through the docs, google ect and trying to understand the photologue source myself but i cant seem to get it to work like i want to.


Solution

  • ImageModel is an abstract class. You can't use it as itself. Instead, you must subclass it:

    class BlogImage(ImageModel):
        pass
    
    class Blog(models.Model):
        ...
        thumbnail = models.ForeignKey(BlogImage, related_name='blogs')
    

    But, the main purpose of ImageModel is to allow you to create a photo model with additional custom data that still behaves like one of photologue's models. photologue already has Photo which is a real model based on ImageModel that you can use if you just need the defaults.

    class Blog(models.Model):
        ...
        thumbnail = models.ForeignKey(Photo, related_name='blogs')