Michael, can you elaborate on what you mean by "forcing bi-directional relationships"?
The ManyToManyField approach really is, I think, the "right" way to do it. If you think about it, a hypothetical ManyToOneField in your case would work almost exactly like the ManyToManyField. The join table would be structured exactly the same, except it would have an additional UNIQUE(photo_id) qualifier. -- Scott On Tue, May 13, 2008 at 2:56 PM, Michael Burton <[EMAIL PROTECTED]> wrote: > > Thanks much, Scott. They both seem a bit hacky, but it gives me > something to work with anyway. > > I recognize the motivation for forcing bi-directional relationships in > Django was done to keep things DRY[1], but does anyone know if there's > been any discussion about maybe relaxing this constraint? Seems a > little restrictive, and I don't think most other web frameworks go > this route for that very reason... > > Mike > > [1] > http://www.djangoproject.com/documentation/db-api/#how-are-the-backward-relationships-possible > > > > > On May 13, 11:32 am, "Scott Moonen" <[EMAIL PROTECTED]> wrote: > > Michael, you have two alternatives: > > > > 1. 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. > > 2. 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/<http://scott.andstuff.org/%7Chttp://truthadorned.org/> > > > -- 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 -~----------~----~----~----~------~----~------~--~---