raw_id_fields: How to show a name instead of id?

前端 未结 4 1615
北恋
北恋 2020-12-13 18:58

Customizing a Django Admin panel, I\'m using raw_id_fields to select a ForeignKey from a Model which has thousands of elements, because the default select-box drop-down is i

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 18:59

    For the representation of an object use __unicode__

    class Person(models.Model):
        first_name = models.CharField(max_length=50)
        last_name = models.CharField(max_length=50)
    
        def __unicode__(self):
            return u'%s %s' % (self.first_name, self.last_name)
    

    In Python 3 use

    def __str__(self):
    

提交回复
热议问题