I would remove the site field in my Photo model and add ForeignKey to Gallery . I would remove limit_choices_to from the photos fields in Gallery .
Since you are using ForeignKey to site s, this means that sites do not exchange galleries and photos. Therefore, the presence of those that I mentioned above is already useless.
class Photo(models.Model): title = models.CharField(max_length=100) gallery = models.ForeignKey(Gallery, related_name='photos') file = models.ImageField(upload_to=get_site_profile_path) def __unicode__(self): return self.title class Gallery(models.Model): name = models.CharField(max_length=40) site = models.ForeignKey(Site) def __unicode__(self): return self.name
Once you set a site in the gallery, all of its photos inherit this property. And the site will be available as photo_instance.gallery.site :
@property def site(self): return self.gallery.site
This should work as if you had a site field. But I did not test it.
Changes or course if you decide that a gallery or photo can appear on several sites.
muhuk
source share