I’m teaching myself Django and creating a personal portfolio site. I’ve setup a model called FolioItem which makes use of a Many-To-Many relation to another model called FolioImage. The idea being each FolioItem can have numerous FolioImages.
class FolioImage(models.Model):
image = models.FileField(upload_to='portfolio_images')
def __unicode__(self):
return self.image.name
class FolioItem(models.Model):
title = models.CharField(max_length=50)
thumbnail = models.FileField(upload_to='portfolio_images')
images = models.ManyToManyField(FolioImage)
def __unicode__(self):
return self.title
In Django’s admin interface I can create new FolioItem’s but the images field (the many-to-many relating to a FolioImage) is showing all existing FolioImage’s added previously to other FolioItems.
Really this should be blank as it is a new item and doesn’t have any images associated yet. Is there any way to force this selector to be blank when creating a new item?
Also, it seems when I attempt to edit an existing entry I can’t add or delete FolioImage associations (the small green + icon is not present).
Any advice would be great.
I think you are using the wrong relationship here. If you only want FolioImages to belong to a single FolioItem, then you should be using a ForeignKey from FolioImage to FolioItem. You can then configure the admin interface to show the related images for an item as inline formsets, which will allow you to add an edit only the images for that item.