0
嗨,我有這個Django的Python代碼,基本上我試圖建立它,以便每個tourobject可以有很多tourbets,但也想能夠保存某個獲勝的tourbet作爲當前下注但然後django抱怨它找不到TourBet兩個外鍵問題django
class TourObject(models.Model):
tourplace = models.ForeignKey(TourPlace, related_name='tourplace', verbose_name="tourplace", default=1)
title = models.CharField(max_length=155, blank=True)
description = models.TextField(blank=True)
date_created = models.DateTimeField(auto_now_add=True)
status = models.BooleanField(default=False, verbose_name="Status")
end_date = models.DateTimeField(blank=True, default=datetime.now)
current_bet = models.IntegerField(blank=True, default=0)
next_bet = models.IntegerField(blank=True, default=0)
no_auction_price = models.IntegerField(blank=True, default=1)
objects = models.Manager()
def __unicode__(self):
return self.title
def get_absolute_url(self):
return reverse("tourobjects:tourobject", kwargs={"pk": self.pk})
class RelatedTourObject(models.Manager):
def get_queryset(self):
return super(RelatedTourObject, self).get_queryset().select_related('tourobject').all()
class TourBet(models.Model):
user = models.ForeignKey(User, related_name='tourbet', verbose_name='User')
tourobject = models.ForeignKey(TourObject, related_name='tourobjectbet', verbose_name="tourobjectbet", default=1)
date_created = models.DateTimeField(blank=True, default=datetime.now)
bet_amount = models.IntegerField(blank=True, default=1)
objects = models.Manager()
related_tourobject = RelatedTourObject()
def __unicode__(self):
return str(self.bet_amount)
謝謝Bipul,我在那裏添加它,並且由於它是衝突的,所以不得不添加related_name。爲此歡呼 –
很好,更新了我的答案。得到upvote或接受支持#SOreadytohelp。 :) –