On May 4, 11:31 am, pbzRPA <pbz...@gmail.com> wrote: > Hi, > > Could you provide the code of your model and why you would want to use > a custom sql instead of the django model object. > > Regards > Pbzrpa
class Stats(models.Model) user = models.ForeignKey(User) variant = models.ForeignKey(PlazaGameVariant) rating = models.IntegerField(null=True, blank=True) The problem is that the id is worthless for sorting in this case since it needs to be sorted by rating and we needed to know the rank of a given user. This was my solution: def GetRank(sTable, sVariantId, sOrderBy, sUserId): strSqlQuery1 = "SET @row=0;" strSqlQuery2 = """ SELECT rank FROM ( SELECT @row:=...@row+1 as rank, user_id FROM ( SELECT user_id FROM %s WHERE variant_id=%s ORDER BY %s DESC ) AS B ) AS A WHERE user_id=%s; """ cursor = connection.cursor() cursor.execute( strSqlQuery1 ) cursor.execute( strSqlQuery2 % (sTable, sVariantId, sOrderBy, sUserId) ) row = cursor.fetchone() return row[0] Initially my problem was trying to execute the two queries in one call, but breaking it out fixes it. Though I'm not sure if it's thread safe or not. Also, thank you Malcolm for your valuable input. Regards, MikeL --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---