def save_clue(clue, p, setter, editor, publisher, puzzle, ptype): if clue.dir == "across": dir = 1 else: dir = 2 try: cl = Clue.objects.get(puzzle__title=p.title, num=clue.num, dir=dir) cl.answer=clue.ans cl.text=smart_str(clue.clue) cl.type=ptype cl.save() try: ans = Answer.objects.get(answer=cl.answer) except ObjectDoesNotExist: ans = Answer(answer=cl.answer, count=1) ans.save() except ObjectDoesNotExist: cl = Clue(setter=setter, puzzle=puzzle, row=clue.row, col=clue.col, num=clue.num, answer=clue.ans, dir=dir, text=clue.clue, type=ptype) try: cl.save() except Exception: t = "" for i in range(len(clue.clue)): if ord(clue.clue[i:i+1]) < 0x80: t += clue.clue[i:i+1] cl.text = t cl.save() try: ans = Answer.objects.get(answer=cl.answer) ans.count += 1 ans.save() except ObjectDoesNotExist: ans = Answer(answer=cl.answer, count=1) ans.save() except MultipleObjectsReturned: print "multiple clues found for %s %s %s" % (clue.ans, clue.clue, p.title) except Exception: t = "" for i in range(len(clue.clue)): if ord(clue.clue[i:i+1]) < 0x80: t += clue.clue[i:i+1] cl.text = t cl.save()
memory. Using the iterator() method only causes it to not preload all the classes. Note that the implementation of the iterator does not support ordered query sets. ''' pk = 0 last_pk = queryset.order_by('-pk')[0].pk queryset = queryset.order_by('pk') while pk < last_pk: for row in queryset.filter(pk__gt=pk)[:chunksize]: pk = row.pk yield row gc.collect() count = 0 q = Clue.objects.only("answer") for cl in queryset_iterator(q.all()): try: ans = Answer.objects.get(answer=cl.answer) ans.count += 1 ans.save() except ObjectDoesNotExist: ans = Answer(answer=cl.answer, count=1) ans.save() except MultipleObjectsReturned: print "multiple clues found for %s" % ans.answer count += 1 if count % 10000 == 0: print count