tags:

views:

67

answers:

2

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.

+1  A: 

Is there any way to force this selector to be blank when creating a new item?

I think you are slightly confused about the interface to the default select multiple widget. It is showing you all the existing images, making them available to you as choices, but there is no relationship on a brand new item. You have to actually click on an item, or do the shift (or control)-click trick to select multiple items, then save your folio item in order to make the relationship.

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

Again, I think you just need to click or ctrl-click to select/deselect the images you want to associate with your item.

You may also be interested in using Django's cool horizontal or vertical filter instead of the default select multiple interface. You can also use the inline interface. More details are here and here. I think the filter interface is much more intuitive.

Brian Neal
+1  A: 

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.

Daniel Roseman
Thanks. I hadn't got to learning how inline models work. That works MUCH better and makes more sense since a single FolioImage will only ever be associated with a single FolioItem.
Groady