我正在寫一個遊戲網站,其中繪製是一系列四位數。例如1234在Django中,如何編寫一個查詢來選擇四個整數的所有可能的組合?
我試圖在django中寫一個查詢,它將根據輸入的四位數字選擇所有的獲勝者,winners是相同數字或相同組合的任意組合,1 2 3 4,2 3 1 4 4 1 3 2都是勝利者。
是怎麼寫這個查詢的最有效方法。
---------------------編輯,很抱歉不能提供模型樣本這裏有如下:-----------
class Draw(models.Model):
digit1 = models.PositiveSmallIntegerField(null=True,blank=True)
digit2 = models.PositiveSmallIntegerField(null=True,blank=True)
digit3 = models.PositiveSmallIntegerField(null=True,blank=True)
digit4 = models.PositiveSmallIntegerField(null=True,blank=True)
draw_date = models.DateTimeField()
closed = models.BooleanField()
winner = models.BooleanField()
def __unicode__(self):
return "Draw For Week Ending %s" %(self.draw_date)
def get_absolute_url(self):
return "/draw/%s/" % (self.draw_date)
def save(self, force_insert=False, force_update=False):
if self.digit1 and self.digit2 and self.digit3 and self.digit4:
#check if there are winners
try:
winners = Ticket.objects.filter(draw=self.id,digit1=self.digit1,digit2=self.digit2,digit3=self.digit3,digit4=self.digit4)
self.winner = True
except Ticket.DoesNotExist:
self.winner = False
#close & save draw/winners
self.closed = True
# Add new Draw for following week.
new_date = self.draw_date + datetime.timedelta(hours=168)
new_draw= Draw(draw_date=new_date)
new_draw.save()
super(Draw, self).save(force_insert, force_update) # Call the "real" save() method.
class Serial(models.Model):
serial = models.CharField(max_length=4)
closed = models.BooleanField(unique=False)
def __unicode__(self):
return "%s" %(self.serial)
def get_absolute_url(self):
return "/draw/serial/%s/" % (self.serial)
class Ticket(models.Model):
draw = models.ForeignKey(Draw)
digit1 = models.PositiveSmallIntegerField()
digit2 = models.PositiveSmallIntegerField()
digit3 = models.PositiveSmallIntegerField()
digit4 = models.PositiveSmallIntegerField()
date = models.DateField(auto_now_add=True,editable=False)
active = models.BooleanField(default=True)
serial_used = models.ForeignKey(Serial,related_name="ticket_serial_used")
def __unicode__(self):
return "#: %s - %s" %(self.id,self.draw)
def get_absolute_url(self):
return "/ticket/%s/" % (self.id)
def save(self, force_insert=False, force_update=False):
if self.serial_used:
serial = Serial.objects.get(pk=self.serial_used.id)
serial.closed = True
serial.save()
super(Ticket, self).save(force_insert, force_update) # Call the "real" save() method.
你的模型在存儲玩家選擇的數字方面是如何設置的? – lemonad 2009-10-27 15:04:46
無法看到您的模型而無法回答。 – 2009-10-27 15:19:41
看到上面的模型聲明,我試圖確定是否他們是贏家當Save方法被重寫在Draw模型 – Rasiel 2009-10-27 15:26:30