A third alternative is to use a GenericForeignKey. Although this may add too much complexity. Put the GenericForeignKey in a model called PhotoTag and create a M2M relationship between it and Photo and use it to select either a Place or a UserProfile, i.e.
class PhotoTag(models.Model) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() tag_object = generic.GenericForeignKey('content_type', 'object_id') class Meta: unique_together = ('content_type', 'object_id') I use this in a similar, but far more complex situation. I have overidden the save in my relevant models (in this case UserProfile and Place) to automatically create the the PhotoTag object when creating new objects. -richard On 5/13/08, Scott Moonen <[EMAIL PROTECTED]> wrote: > Michael, you have two alternatives: > > Create ManyToManyField fields in the UserProfile and Place models, pointing > to Photo. "ManyToManyField" may seem a bit odd since you really have a > many-to-one relation, but it will work as you expect, creating a join table > connecting each pair of models. > Create two ForeignKey fields in Photo, one to UserProfile and one to Photo, > with null=True. Yes, this is a bit ugly. :) > -- Scott > > On Tue, May 13, 2008 at 2:24 PM, Michael Burton <[EMAIL PROTECTED]> wrote: > > > > I have some Places and I have some Users in my database. I'd like to > > be able to associate some Photos with each. > > > > class Photo(models.Model): > > # a model that represents a photo > > > > class UserProfile(models.Model): > > # has a list of Photos > > > > class Place(models.Model): > > # has a list of Photos > > > > > > Normally, if i were using another ORM framework, I would make my Place > > have a list of photos, and I'd make my UserProfile have a list of > > photos, and I'd leave my Photo model alone. However, the Django way > > of doing things requires that I put a ForeignKey into my Photo model > > to establish the one-to-many. > > > > The problem is, sometimes Photo's ForeignKey will point to a > > UserProfile and sometimes to an Place. How can I have both my > > UserProfile and Place models point to Photos? > > > > Thanks in advance, > > Mike > > > > > > > > -- > http://scott.andstuff.org/ | http://truthadorned.org/ > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---