Django ModelChoiceField queryset不从数据库返回实际值
我的型号是:Django ModelChoiceField queryset不从数据库返回实际值,django,django-models,django-forms,django-queryset,Django,Django Models,Django Forms,Django Queryset,我的型号是: class ActionType(models.Model): id_action_type = models.FloatField(primary_key=True) action_name = models.CharField(max_length=15, blank=True, null=True) class Meta: managed = False db_table = 'action_type' cla
class ActionType(models.Model):
id_action_type = models.FloatField(primary_key=True)
action_name = models.CharField(max_length=15, blank=True, null=True)
class Meta:
managed = False
db_table = 'action_type'
class TicketsForm(models.Model):
ticket_id = models.FloatField(primary_key=True)
ticket_type = models.CharField(max_length=30, blank=True, null=True)
action_type = models.CharField(max_length=15,blank=True, null=True)
在我的表格中,我有:class BankForm(forms.ModelForm):
action_type= forms.ModelChoiceField(queryset=ActionType.objects.all(),widget=forms.RadioSelect)
class Meta:
model = TicketsForm
fields = ('ticket_type',
'action_type',)
将其呈现为html时,我看不到ActionType.objects.all()的实际值,而是看到
ActionType对象
ActionType对象
靠近RadioButton。
谁能告诉我我的错误在哪里。您需要为您的模型定义一个\uu str\uu
方法。例如:
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class ActionType(models.Model):
id_action_type = models.FloatField(primary_key=True)
action_name = models.CharField(max_length=15, blank=True, null=True)
...
def __str__(self)
return self.action_name
只有在使用python 2时,才需要与python 2\u unicode\u兼容的decorator。有关更多信息,请参阅文档。您需要为您的模型定义一个\uuuu str\uuu
方法。例如:
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class ActionType(models.Model):
id_action_type = models.FloatField(primary_key=True)
action_name = models.CharField(max_length=15, blank=True, null=True)
...
def __str__(self)
return self.action_name
只有在使用python 2时,才需要与python 2\u unicode\u兼容的decorator。有关更多信息,请参阅文档。我想这是因为在ActionType模型上没有定义\uuu str\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuself)
方法。有关详细信息,请参阅
但是,我强烈建议在TicketsForm
到ActionType
中使用外键。而且,我不确定什么是定义自己的私钥的必要条件;如果您不定义这些,Django将为您生成它们。有关更多详细信息(尤其是模型方面的信息),请参阅教程。我想这是因为您没有在ActionType模型上定义\uuu str\uuuuuuuuuuuuuuself)
或\uuuuuuuuuuuunicode\uuuuuuuuuuuself)
方法。有关详细信息,请参阅
但是,我强烈建议在TicketsForm
到ActionType
中使用外键。而且,我不确定什么是定义自己的私钥的必要条件;如果您不定义这些,Django将为您生成它们。有关更多详细信息,请参见本教程(尤其是关于型号)。请投票人评论并说明您返回参考的原因。为了避免这种情况,您必须定义str,它设置您在调用对象时返回的内容。请向下的投票者评论并说明您返回引用的原因。为了避免这种情况,必须定义str,它设置调用对象时返回的内容。