zoukankan      html  css  js  c++  java
  • django admin下拉列表不显示值,显示为object的处理

    问题:模板中创建form表单中的下拉列表, 前台打开页面显示object,而不是值,如图:

    尝试了多种办法无果,最后解决了,处理办法是修改models.py,原来的model:

    [python] view plain copy
    1. class TechnicistLocation(models.Model):  # 技术人员位置  
    2.     LocationName = models.CharField('位置名称', max_length=20)  
    3.   
    4.     class Meta:  
    5.         verbose_name_plural = '技术人员位置'  
    6.         app_label ="schedule"  
    7.   
    8.     def __unicode__(self):  
    9.         return self.LocationName  


    修改后的model:

    [python] view plain copy 在CODE上查看代码片派生到我的代码片
    1. class TechnicistLocation(models.Model):  # 技术人员位置  
    2.     LocationName = models.CharField('位置名称', max_length=20)  
    3.   
    4.     class Meta:  
    5.         verbose_name_plural = '技术人员位置'  
    6.         app_label ="schedule"  
    7.   
    8.     def __str__(self):  
    9.         return self.LocationName     

    区别就在倒数第二行,python3直接使用__str__(self)就可以了,如果是python2,则要用__unicode__(self)

  • 相关阅读:
    全民医疗
    SpringMVC
    Mybatis 缓存策略
    不要追涨杀跌
    我只认比特币
    ETH反思
    世界是熵增的
    切片最好还是传引用
    rxgo示例
    11月份的计划
  • 原文地址:https://www.cnblogs.com/zknublx/p/6150611.html
Copyright © 2011-2022 走看看