A more complete explanation: Ordering on the related table works fine as long as all the articles have an associated ArticleRank (article_rank.rank, depending on how you look at it). > order_by = '-articlerank__rank' > art_list = Article.objects.filter(\ > title__icontains=term, status='PUBLISHED')\ > .distance(pnt)\ > .order_by(order_by) A search that includes articles with no entry in the ArticleRank table *does* include them in the result set, but puts them *first* in the ordering results. Then, it includes the rest of the correctly ordered results. This is a little confusing. If I try to access the article's rank (one with a missing ranking) I get an error. So, I'm trapping the exception like this: try: article_rank = art.articlerank.rank except ArticleRank.DoesNotExist: article_rank = 0
That's okay, but the ordering is still a problem. I want it to treat missing rank as if it were 0 and put those at the bottom of the list. I suppose I could just add the articles with no ranking to the article_rank table and give them a ranking of 0. Is there a better way? 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 -~----------~----~----~----~------~----~------~--~---