模型表单下拉 (select) 字段未显示可供选择的正确行
Model Form drop-down (select) field not displaying correct rows to choose from
我有一个 Parent Model
,它使用指向 Child Model
的 Foreign Key
。在这种情况下,"Child Model" 称为 Mstrgensalutationtype(实际上是称呼)。
Parent Model
正在用于创建 Model Form
基本上,下面是我在尝试选择称呼类型时得到的结果。
我要看的是
Mr.
Ms.
Mrs.
Prof.
Dr.
问题:我做错了什么?
TIA
models.py - 用作子模型
class Mstrgensalutationtype(models.Model):
saltypeid = models.BigIntegerField(primary_key=True)
lang = models.CharField(max_length=2, blank=True, null=True)
shortval = models.CharField(max_length=7, blank=True, null=True)
salutationlong = models.CharField(max_length=20, blank=True, null=True)
class Meta:
managed = False
db_table = 'MstrGenSalutationType'
def __unicode__(self):
return u'%s ' % ( self.shortval )
models.py - 用作父模型
class Mstrstorehead(models.Model):
tenantid = models.BigIntegerField(primary_key=True)
extrefacctno = models.CharField(max_length=20, blank=True, null=True, verbose_name="Account Reference No")
[... snip ...]
contactsalutationid = models.ForeignKey(Mstrgensalutationtype, models.DO_NOTHING, db_column='contactsalutationid', blank=True, null=True, verbose_name="Salutation")
[... snip ...]
class Meta:
managed = False
db_table = 'MstrStoreHead'
更新
我在下面进行了以下更改 - 但仍然遇到同样的问题。
class Mstrgensalutationtype(models.Model):
saltypeid = models.BigIntegerField(primary_key=True)
lang = models.CharField(max_length=2, blank=True, null=True)
shortval = models.CharField(max_length=7, blank=True, null=True)
salutationlong = models.CharField(max_length=20, blank=True, null=True)
class Meta:
managed = False
db_table = 'MstrGenSalutationType'
def __unicode__(self):
return u'%s ' % ( self.shortval )
__unicode__
方法仅在Python2 - it doesn't do anything in Python 3有效。您需要使用 __str__
代替:
def __str__(self):
return self.shortval
另请注意,u
字符串前缀在 Python 中是多余的 3. 默认情况下,所有字符串都是 unicode。
如果您需要同时支持 Python 2 和 3,请按照上述 link 中的说明使用 python_2_unicode_compatible
。
我有一个 Parent Model
,它使用指向 Child Model
的 Foreign Key
。在这种情况下,"Child Model" 称为 Mstrgensalutationtype(实际上是称呼)。
Parent Model
正在用于创建 Model Form
基本上,下面是我在尝试选择称呼类型时得到的结果。
我要看的是
Mr.
Ms.
Mrs.
Prof.
Dr.
问题:我做错了什么?
TIA
models.py - 用作子模型
class Mstrgensalutationtype(models.Model):
saltypeid = models.BigIntegerField(primary_key=True)
lang = models.CharField(max_length=2, blank=True, null=True)
shortval = models.CharField(max_length=7, blank=True, null=True)
salutationlong = models.CharField(max_length=20, blank=True, null=True)
class Meta:
managed = False
db_table = 'MstrGenSalutationType'
def __unicode__(self):
return u'%s ' % ( self.shortval )
models.py - 用作父模型
class Mstrstorehead(models.Model):
tenantid = models.BigIntegerField(primary_key=True)
extrefacctno = models.CharField(max_length=20, blank=True, null=True, verbose_name="Account Reference No")
[... snip ...]
contactsalutationid = models.ForeignKey(Mstrgensalutationtype, models.DO_NOTHING, db_column='contactsalutationid', blank=True, null=True, verbose_name="Salutation")
[... snip ...]
class Meta:
managed = False
db_table = 'MstrStoreHead'
更新
我在下面进行了以下更改 - 但仍然遇到同样的问题。
class Mstrgensalutationtype(models.Model):
saltypeid = models.BigIntegerField(primary_key=True)
lang = models.CharField(max_length=2, blank=True, null=True)
shortval = models.CharField(max_length=7, blank=True, null=True)
salutationlong = models.CharField(max_length=20, blank=True, null=True)
class Meta:
managed = False
db_table = 'MstrGenSalutationType'
def __unicode__(self):
return u'%s ' % ( self.shortval )
__unicode__
方法仅在Python2 - it doesn't do anything in Python 3有效。您需要使用 __str__
代替:
def __str__(self):
return self.shortval
另请注意,u
字符串前缀在 Python 中是多余的 3. 默认情况下,所有字符串都是 unicode。
如果您需要同时支持 Python 2 和 3,请按照上述 link 中的说明使用 python_2_unicode_compatible
。