Below I call the same methods in author and w5 model objects. But one of them raises an error:
>>> author = models.Author('ali')
>>> author.article_set.count()
---------------------------------------------
ValueError: invalid literal for int() with base 10: 'ali'
>>> w5 = models.Author(name='w5')
>>> w5.article_set.count()
0
Actually, before these lines I had previously a wrong Author class definition. I obtained the ValueError from author object first with that former definition of Author. Then I changed Author class and reloaded the modules.
After reloading the models with the reloadmodels.py written by Chad Braun-Duin, newly instantiated objects like w5 work properly. But reinstantiated objects like author raise errors.
Is this contradictory behavior due to django’s query caching logic or reloadmodels.py? Have any idea?
Thanks…
This isn’t anything to do with Django, it’s a Python thing. In the linked question, Chad was importing models like this:
Using this syntax, you can use
reload()to refresh class definitions when you change them on disk. However, it’s far more standard to import your models like this:If you do this – and most people do – reload() has no effect, even with Chad’s hack.
Really, it’s more simple to just quit the Python shell and restart it – especially if you use
shell_plusfrom django-extensions which automatically loads your models into the shell on startup.