简体   繁体   English

Django中奇怪的“ DoesNotExist”异常

[英]Strange “DoesNotExist” exception in Django

There is a simple code: 有一个简单的代码:

    class City(models.Model):
        slug = models.SlugField(max_length=150, unique=True)
        name = models.CharField(max_length=255)


    class CityPart(models.Model):
        city = models.ForeignKey('City', related_name='rel_cityparts')
        slug = models.SlugField(max_length=150, unique=True)
        name = models.CharField(max_length=255)

        def __unicode__(self):
            #return self.name
            return u'%s (%s)' % (self.name, self.city.name)

I found that in __unicode__ method fails, leading to DoesNotExist exception. 我发现__unicode__方法失败,导致DoesNotExist异常。 Django "thinks" the related city does not exist, thought it exists for sure. Django“认为”相关城市不存在,以为肯定存在。 I have re-checked it, the DB is valid. 我已经重新检查了一下,该数据库是有效的。 I used Sqlite and Postgres, both loads\\dumps the DB data but both raises this exception. 我使用了Sqlite和Postgres,它们都加载\\转储数据库数据,但是都引发了此异常。 So I think it's related to some code issues, not to the DB inconsistency. 因此,我认为这与某些代码问题有关,而不是与数据库不一致有关。

The full traceback is here (really uninformative): http://dpaste.com/hold/1429873/ 完整的追溯在这里(真的没有信息): http : //dpaste.com/hold/1429873/

When I'm checking the value in the file virtenv_macos/lib/python2.7/site-packages/debug_toolbar/panels/template.py , line 85 (*pformat(value)*) - it tells me that the "value" variable has the following value: *[broken repr (DoesNotExist)]* 当我检查文件virtenv_macos/lib/python2.7/site-packages/debug_toolbar/panels/template.py ,第85行(*pformat(value)*) -它告诉我“ value”变量具有以下值: *[broken repr (DoesNotExist)]*

MacOS, Python 2.7, Django 1.5.2 MacOS,Python 2.7,Django 1.5.2

PS The exception raises when I'm trying to see the list of DB records (ListView), not when I'm trying to create a new one. PS:当我尝试查看数据库记录列表(ListView)时,而不是在尝试创建一个新记录时,会引发异常。

I think you shoud see this post. 我认为您应该看到这篇文章。

If you are trying in create a CityPart instance and call the ____unicode____ method and you haven't set the city, it'll raise an exception. 如果您尝试创建CityPart实例并调用____unicode____方法,但尚未设置城市,它将引发异常。

your bad code could be: 您的错误代码可能是:

c = CityPart()
c  # This fail because you don't set the City!


#  try that
city = City(name='foo', slug='fooslug')
city.save() 
c = CityPart(city=city, name='bar')
c  # MAYBE this works, maybe

try to do not use the city in the ____unicode____ method: 尝试不要在____unicode____方法中使用城市:

class City(models.Model):
    slug = models.SlugField(max_length=150, unique=True)
    name = models.CharField(max_length=255)


class CityPart(models.Model):
    city = models.ForeignKey('City', related_name='rel_cityparts')
    slug = models.SlugField(max_length=150, unique=True)
    name = models.CharField(max_length=255)

    def __unicode__(self):
        #return self.name
        return u'%s' % (self.name,)

Sorry my english. 对不起,我的英语。 Hope helps! 希望有帮助!

It was because of the django-debug-toolbar app, it's "debug_toolbar.middleware.DebugToolbarMiddleware" middleware. 这是因为django-debug-toolbar应用程序,它是“ debug_toolbar.middleware.DebugToolbarMiddleware”中间件。 Don't have time to debug it, unfortunately. 不幸的是,没有时间调试它。 Just commented it out. 只是注释掉了。

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

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