pythondjangopostgresqlphotophotologue

Using django-photologue to assign single image to a model


I'm working on my first project in django, and the first part of it is a blog app. I've installed django-photologue which seems very useful for the intended purposes for later apps. Although this app will be used extensively later for posting galleries in other apps, assigning single photo to a model still eludes me. Namely, what I want is to assign a single image to each post, which will be a featured image to show on generated list of posts. I've tried a couple of different way, always resulting in same error.

First attempt:

class Post(models.Model):
    ...
    image = models.ForeignKey(Photo, related_name='posts')
    ...

Migrating worked and the option appeared in post admin. It showed a drop down list of all images in photologue library. I tried to create a test post in admin and it resulted in a ProgrammingError

column posts_post.image_id does not exist
LINE 1: ..., "posts_post"."created", "posts_post"."updated", "posts_pos...

From what I understand, trying to create a relation falls through because it lacks the image_id column to assign the pk (although I'm not 100% sure that's the problem).

I experienced this same error when I tried creating categories for posts, which I solved using ManyToManyField so I gave it a try.

Second attempt:

class Post(models.Model):
    ...
    image = models.ManyToManyField(Photo, blank=False, through='ImageToPost')
    ...

class ImageToPost(models.Model):
    post = models.ForeignKey(Post)
    image = models.ForeignKey(Photo)

After updating the model, makemigrations went through without errors, but doing migrate yields similar error:

psycopg2.ProgrammingError: column "image_id" of relation "posts_post" does not exist

Again, the programming error from postgree pops up. Right now my knowledge of django is limited, so trying to go through photologue models confuses me even further. Any suggestion on how to properly set up my model or an example of models using Photo to assign a single image from photologue to it?

A couple notes:

1) The dropdown list from post admin showed all images stored by photologue. This solution is not very productive, as the site will store extensive amount of images, and very few of those will be related to blog posts. Is there a way to filter this list to a single gallery?

2) Second solution with ManyToManyField is also redundant. Namely, it mirrors the category function which allows posts to be related to one or more post categories, and allow categories to related to any amount of blog posts. For featured images this is not necessary, each post must be related to a single image (multiple posts can link to a same image), but images don't need to be related to blog posts, which makes ManyToManyField perhaps a wrong one to use, but I'm not sure weather to use OneToOneField or ManyToOneField? Does OneToOne relation prevents multiple posts to link to a same image?


Solution

  • As per Andreas' comment: it looks like your database does not reflect your Post model; it looks like either you have migrations that have not been applied to the database (run python manage.py migrate --list to see them), or else you have not yet created migrations for your Post model - refer to the migrations documentation for more information.

    About your 'couple notes':

    1) Is the single gallery going to be hard-coded? If yes, then in the admin you can filter down the list of image instances to be displayed in the list - look for formfield_for_foreignkey() on that page.

    2) A OneToOne field will enforce one post to one image; this may, or may not, be what you want!