Using django-photologue to assign single image to a model
<p>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.</p>

<p>First attempt:</p>

<pre><code>class Post(models.Model):
    ...
    image = models.ForeignKey(Photo, related_name='posts')
    ...
</code></pre>

<p>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</p>

<pre><code>column posts_post.image_id does not exist
LINE 1: ..., "posts_post"."created", "posts_post"."updated", "posts_pos...
</code></pre>

<p>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).</p>

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

<p>Second attempt:</p>

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

class ImageToPost(models.Model):
    post = models.ForeignKey(Post)
    image = models.ForeignKey(Photo)
</code></pre>

<p>After updating the model, <code>makemigrations</code> went through without errors, but doing <code>migrate</code> yields similar error:</p>

<pre><code>psycopg2.ProgrammingError: column "image_id" of relation "posts_post" does not exist
</code></pre>

<p>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?</p>

<p>A couple notes:</p>

<p>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?</p>

<p>2) Second solution with <code>ManyToManyField</code> 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 <code>ManyToManyField</code> perhaps a wrong one to use, but I'm not sure weather to use <code>OneToOneField</code> or <code>ManyToOneField</code>? Does OneToOne relation prevents multiple posts to link to a same image?</p>

<p>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 <code>python manage.py migrate --list</code> to see them), or else you have not yet created migrations for your Post model - <a href="https://docs.djangoproject.com/en/1.10/topics/migrations/" rel="nofollow">refer to the migrations documentation for more information</a>.</p>

<p>About your 'couple notes':</p>

<p>1) Is the single gallery going to be hard-coded? If yes, then <a href="https://docs.djangoproject.com/en/1.10/ref/contrib/admin/" rel="nofollow">in the admin you can filter down the list of image instances to be displayed in the list</a> - look for <code>formfield_for_foreignkey()</code> on that page.</p>

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

python django postgresql photo photologue