It is probably better to use candidates.count(), rather than len(candidates). len will cause the whole queryset to be loaded into memory. It doesn't look like you use it later, so that is kind of a waste.
Hope that helps, Alex >From django docs: Note: Don't use len() on QuerySets if all you want to do is determine the number of records in the set. It's much more efficient to handle a count at the database level, using SQL's SELECT COUNT(*), and Django provides a count() method for precisely this reason. See count() below. On Jun 9, 1:24 pm, Nick <[email protected]> wrote: > Just to let you know, this code worked out nicely. > > I made a few changes to the bottom save function: > > if len(candidates) > 1: > raise Exception > candidates.update(incumbent=True) > > On Jun 8, 6:11 pm, Nick <[email protected]> wrote: > > > Thanks Dan, I'll give it a shot > > > On Jun 8, 6:00 pm, Dan Harris <[email protected]> wrote: > > > > Perhaps not the greatest way of doing things, but simple to code/read: > > > > class Candidate(models.Model): > > > first_name = models.CharField(max_length=30) > > > last_name = models.CharField(max_length=30) > > > incumbent = models.BooleanField() > > > > class HoldsOffice(models.Model): > > > first_name = models.CharField(max_length=30) > > > last_name = models.CharField(max_length=30) > > > > for officer in HoldsOffice.objects.all(): > > > candidates = > > > Candidate.objects.filter(first_name__iequals=officer.first_name).filter(last_name__iequals=officer.last_name) > > > > if len(candidates)>0: > > > raise Exception("More than 1 match found") > > > candidates[0].incumbent = True > > > candidates[0].save() > > > > Something like that might work for you assuming that the models and > > > stuff are similar. Also, this code is just off the top of my head, so > > > who knows if it will actually work :) > > > > Cheers, > > > > Dan Harris > > > [email protected] > > > > On Jun 8, 6:30 pm, Nick <[email protected]> wrote: > > > > > I have two models. One is a list of candidates that have filed to run > > > > for office. The second is a list of people who currently hold > > > > office. > > > > > I'd like to compare the two tables and whenever a match is found > > > > between the two (an entry in the candidate table shares the same > > > > last_name and first_name with an office holder from the holds office > > > > table) I'd like to check off a box in the Candidate table called > > > > 'incumbent'. > > > > > how would I begin to do this. They both have columns called last_name > > > > and first_name that I can use to compare but I don't know the syntax. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected]. 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.

