当我将模型实例传递给表单但我想要其他字段时,只有外键id才会反复

时间:2017-06-03 13:38:45

标签: django django-models django-forms django-templates django-views

我是django的新手,我在表单中传递时遇到问题。因为我的实例有10个字段,包括3个外键字段。 当我将实例传递给视图中的表单时,我只看到外键字段属性只返回它们的id,但我想从外键表中检索其他字段。 如果有可能请帮助我。

这是我用作传递表单

的实例的模型
class CreateMasterBet(models.Model):
     owner = models.ForeignKey(User, null=True, related_name='owner')
     opposite = models.ForeignKey(User, null=True, 
     related_name='opposite')
     ownerTeam = models.CharField(max_length=100, default='')
     oppositeTeam = models.CharField(max_length=100, default='')
     match = models.ForeignKey(Match)
     ownerProposedRate = models.IntegerField()
     ownerProposedRateForOpposite = models.IntegerField()
     betis = models.CharField(max_length=20, default='open')
     howMany = models.IntegerField(default=1)
     time = models.DateTimeField(default=now)

     def __str__(self):
         return str(self.owner) + "( " + str(self.ownerTeam) + " )"

此模型实例的表单如下:

class BetEditForm(forms.ModelForm):
          owner = forms.CharField(max_length=100, required=False, 
                widget=forms.TextInput(attrs={'placeholder': 'Owner', 
                'class': 'form-control'}))
          opposite = forms.CharField(max_length=100, required=False, 
                widget=forms.TextInput(attrs={'placeholder': 'Opposite', 
                'class': 'form-control'}))
          ownerTeam = forms.CharField(max_length=100, required=False, 
                widget=forms.TextInput(attrs={'placeholder': 'Owner Team', 
                'class': 'form-control'}))
          oppositeTeam = forms.CharField(max_length=100, required=False, 
                widget=forms.TextInput(attrs={'placeholder': 'Opposite 
                Team', 'class': 'form-control'}))
          match = forms.CharField(max_length=100, required=False,  
                widget=forms.TextInput(attrs={'placeholder': 'Match', 
                'class': 'form-control'}))
          ownerProposedRate = forms.IntegerField(required=False, 
                widget=forms.TextInput(attrs={'placeholder': 'Owner need 
                to pay', 'class': 'form-control'}))
          ownerProposedRateForOpposite = 
                forms.IntegerField(required=False, 
                widget=forms.TextInput(attrs={'placeholder': 'Opposite 
                team pay me', 'class': 'form-control'}))
          betis = forms.CharField(max_length=20, required=False, 
                widget=forms.TextInput(attrs={'placeholder': 'Bet Status', 
                'class': 'form-control'}))
          howMany = forms.IntegerField(required=False, 
                widget=forms.TextInput(attrs={'placeholder': 'Number of 
                Bets', 'class': 'form-control'}))
          time = forms.DateTimeField(required=False, 
                widget=forms.TextInput(attrs={'placeholder': 'Match Played 
                At', 'class': 'form-control'}))

          class Meta:
               model = CreateMasterBet
               fields = ('owner', 'opposite', 'ownerTeam', 'oppositeTeam', 
                         'match', 'ownerProposedRate', 
                         'ownerProposedRateForOpposite', 'betis', 
                         'howMany', 'time')
在views.py中

我传递了表单中的模型实例;

instance = CreateMasterBet.objects.get(id=betid)
form = BetEditForm(request.POST or None, instance=instance)

然后在模板中我只看到相应的外键字段只检索id,但在这个地方我想从外键表中检索其他信息

image link

在该图像中,第2个字段仅显示外键。但我想在这里从外键表中检索其他值

如果您知道该怎么做,请提供帮助。 提前谢谢

0 个答案:

没有答案