简体   繁体   English

Django Unicode无法正常工作

[英]Django unicode is not working

im newbie at Python writing, and I'm trying to set up my database to manage it in django admin panel. 我是Python写作的新手,我正在尝试在Django管理面板中设置数据库来对其进行管理。 My problem is that when i define unicode it's not working and I don't know why. 我的问题是,当我定义unicode时,它不起作用,我也不知道为什么。

class Doors(models.Model):
door_uid = models.IntegerField(primary_key=True)
door_owner = models.IntegerField()
door_ownertype = models.IntegerField()
door_name = models.CharField(max_length=32)

  class Meta:
    verbose_name = 'Drzwi'
    verbose_name_plural = 'Drzwi'
    managed = True
    db_table = 'hrp_doors'

    def __unicode__(self):
        return self.door_uid

after doing it, it still shows 'HrpDoors object'. 完成后,它仍然显示“ HrpDoors对象”。 What am I doing wrong? 我究竟做错了什么?

Not sure about why you get an HrpDoors object. 不确定为什么会得到HrpDoors对象。

You may have an indentation problem. 您可能有缩进问题。 Also your unicode function is returning an integer: 另外,您的unicode函数还返回一个整数:

class HrpDoors(models.Model):
    door_uid = models.IntegerField(primary_key=True)
    ...

    class Meta:
        ...

    def __unicode__(self):
        return unicode(self.door_uid)

In your case, the __unicode__ attribute is a method of class Meta where as it should be for HrpDoors 在你的情况下, __unicode__属性的方法class Meta ,其中,因为它应该是HrpDoors

Try this: 尝试这个:

class HrpDoors(models.Model):
    door_uid = models.IntegerField(primary_key=True)
    door_owner = models.IntegerField()
    door_ownertype = models.IntegerField()
    door_name = models.CharField(max_length=32)

    class Meta:
        verbose_name = 'Drzwi'
        verbose_name_plural = 'Drzwi'
        managed = True
        db_table = 'hrp_doors'

    def __unicode__(self): #Look at the indentation of unicode - same level as attributes of class model
        return u'%s' % self.door_uid #Also, return unicode explicitly

Here is the relevant documentation 这是相关的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM