2

Ok, so here's a simplified version of the thing that has been driving me crazy all day:

models.py:

from django.template.defaultfilters import slugify
class MyModel(models.Model):
    ....

    def populate_property(self):
        self.property = slugify('string of text')

My view then calls populate_property() but I get a NoneType object is not callable error.

BUT, if I add an inline import of slugify then it works.

This is code that has worked for a long time, so I'm at a loss right now.

The PYTHONPATH but it looks ok and the environment is created using buildout

Any help would be much appreciated...

jamstooks
  • 1,425
  • 2
  • 14
  • 22

3 Answers3

1

Having just conquered a similar issue, it sounds like you have a circular reference in your code (this file imports something that imports from this file). There's no harm in putting the import inside populate_property.

Tom
  • 22,301
  • 5
  • 63
  • 96
  • to be true it means that the circular import is caused somewhere in django.template.defaultfilters, it sounds a bit strange to me ... he also says that it happens when the method is called and not when the module is loaded. – Tommaso Barbugli Jan 03 '12 at 20:54
  • That was essentially it, actually. Thanks, Tom. I was using a wildcard include where it wasn't necessary and that killed just about everything. [I should know better](http://stackoverflow.com/questions/3615125/should-wildcard-import-be-avoided). – jamstooks Jan 04 '12 at 15:27
0

If you are using Django < 1.7, I noticed this is almost certainly due to a circular import caused by an import signal. I always ended up with quirky situations where it works in local, maybe on dev server and probably not on prod server...

Vincent Alvo
  • 155
  • 1
  • 2
  • 9
0

Based on the code you've posted, there should be no problems. The only thing I can think of to cause the error you mention is that a some point you changed the value of slugify. It was most likely accidental use of = when intending to use == or something similar.

The best way to check is to leave the rest of your code as is, but in addition to the import you have now, add from django.template import defaultfilters. Then, change the specific line of code in question to:

def populate_property(self):
    self.property = defaultfilters.slugify('string of text')

If it works, then try to track down the place where slugify got reassigned.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thanks for the suggestion, Chris. `defaultfilters` is also undefined. In fact most of the things that are not inline includes are undefined. – jamstooks Jan 04 '12 at 13:17
  • Is it only Django imports? Perhaps something went awry in Django package -- reinstalling it may help. If this behavior is occurring with stdlib imports as well, you whole environment may be borked. Do you have another system that you can test your code on? (Or try creating a new virtualenv if you're using that). – Chris Pratt Jan 04 '12 at 15:11