Malcolm, Thanks for pointing that out! That set me on the right course, and I was able to get things working with _ArticleRank_ this time. I have a follow-up, though. Not every article has been assigned a rank. When a search includes an article without a rank I get an error. I can just trap the exception, but... is there some better way of dealing with this? It's okay to just assign articles with no rank the rank of 0.
Anyway, thanks so much for your prompt and very helpful response! Liam Malcolm Tredinnick wrote: > (You forgot to include a title. I nearly nuked this as spam, by > accident. So I've thrown in a title for fun.) > > On Sun, 2008-12-14 at 19:50 -0800, Info Cascade wrote: > >> Hi -- >> >> I have a question about many-to-many relationships and how to reference >> a related object field using Django syntax via the Manager class. >> I think this is probably pretty simple and just reflects my inexperience >> working with Django (which overall I am enjoying, btw.) >> Anyway, the models look like this: >> >> class Poi(models.Model): # A "Point of Interest" >> id = models.AutoField(primary_key=True) >> > > By the way, you can leave this out. Django will automatically create a > field with that name for you that is the primary key. > > >> title = models.CharField(max_length=1024, null=False, db_index=True) >> articles = models.ManyToManyField('Article', null=True, >> db_table='poi_articles') >> >> class PoiRank(models.Model): >> poi = models.OneToOneField(Poi, primary_key=True) >> rank = models.IntegerField(null = False, blank = False) >> >> class Article(models.Model): >> id = models.AutoField(primary_key=True) >> title = models.CharField(max_length=1024, blank=True, db_index=True) >> pois = models.ManyToManyField(Poi, null=True, db_table='poi_articles') >> status = models.CharField(max_length=27, blank=True) >> >> >> One Poi can have many Articles written about it. One Article can be >> referenced by many Poi's. >> A Poi has 0 or 1 "rank" value which is some measure of its popularity. >> >> Now, I had to set the db_table name to poi_articles because otherwise I >> ended up with what were essentially duplicate tables (just with the >> order of the foreign keys swapped). >> Of course, doing that I then had to remove the duplicate poi_articles >> table that got generated the first time I ran syncdb. >> This makes me think that I may not have set things up correctly in the >> first place. >> > > I don't see anything in your explanation that requires having a > ManyToManyField on both Article and Poi. Just put the ManyToMany on one > of the models and you can still use it to refer back and forth. Django's > relation fields are created nicely so that you only need to specify one > end, not both. What you have done to hack around this sounds like it > might lead to problems later. > > For argument's sake, I'll assume you get rid of the "pois" attribute on > the Article table > > >> Anyway, after doing a query I have a QuerySet of articles. I need to >> know that the rank of the article is. That's all. It should be simple, >> right? >> > > The problem is that your requirement is not well-defined. If you have > any particular single Article object, there can be many Poi objects > related to it (that was part of your problem description), each one > having a different rank. There's no single rank value associated with a > single Article. > > Regards, > Malcolm > > > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---