1

I'm experimenting with Page extensions and FeinCMS 1.2.1.

I'd like to create this simple page extension:

from django.db import models

def register(cls, admin_cls):
    cls.add_to_class(models.TextField())

but I keep getting this error:

AttributeError: 'module' object has no attribute 'TextField'

The package containing the extension is called dev.extensions and the module categories, I have added it to the Python path and the extension is registered with Page.register_extensions('dev.extensions.categories').

I'm wondering what I'm doing wrong, please help.

The complete stacktrace follows.

c:\sandbox\projects\feindev>python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 13, in <module>
    execute_manager(settings)
  File "c:\sandbox\environments\feincms-1.2
.1\lib\site-packages\django\core\management\__init__.py", line 436, in execute_m
anager
    setup_environ(settings_mod)
  File "c:\sandbox\environments\feincms-1.2
.1\lib\site-packages\django\core\management\__init__.py", line 419, in setup_env
iron
    project_module = import_module(project_name)
  File "c:\sandbox\environments\feincms-1.2
.1\lib\site-packages\django\utils\importlib.py", line 35, in import_module
    __import__(name)
  File "c:\sandbox\projects\feindev\..\fein
dev\__init__.py", line 52, in <module>
    Page.register_extensions('dev.extensions.types')
  File "c:\sandbox\environments\feincms-1.2
.1\lib\site-packages\feincms\models.py", line 270, in register_extensions
    cls.register_extension(fn)
  File "c:\sandbox\environments\feincms-1.2
.1\lib\site-packages\feincms\module\page\models.py", line 564, in register_exten
sion
    register_fn(cls, PageAdmin)
  File "c:\sandbox\projects\feindev\dev\ext
ensions\types\__init__.py", line 4, in register
    cls.add_to_class(models.TextField())
AttributeError: 'module' object has no attribute 'TextField'
Paolo
  • 20,112
  • 21
  • 72
  • 113
  • What happens if you do print(dir(models)) right before the line that is causing you a problem? and if you do print(models.__package__) ?? – Tom Gruner Mar 13 '11 at 00:31
  • `['__builtins__', '__doc__', '__file__', '__name__', '__package__']` and `None` – Paolo Mar 13 '11 at 00:49

1 Answers1

3

Maybe models is getting reassigned somewhere to another module before your register function gets run?

Try changing your code to this:

from django.db import models as django_models

def register(cls, admin_cls):
    cls.add_to_class('field_name_here', django_models.TextField())
Tom Gruner
  • 9,635
  • 1
  • 20
  • 26
  • Thanks, that was the problem! Now it says `TypeError: add_to_class() takes exactly 3 arguments (2 given)`, but that's fine, it is because a first positional argument is required. Btw, I'm just wondering where the mess is happening... Do you have any hint for this too? :) – Paolo Mar 13 '11 at 01:02
  • I am not familiar with feincms, but I imagine there must be some scope issue with the way that feincms imports your extension. When the register method gets imported into another module, the scope of models must get redefined at somepoint? That is just a guess, as I still need to study exactly how scope works in python. – Tom Gruner Mar 13 '11 at 01:11
  • Problem localized, Page.register_extensions has to be called before Page.register_templates. Though, even looking register_templates [source code](https://github.com/matthiask/feincms/blob/v1.2.1/feincms/models.py#L171) I can't figure why. But that probably is my limit! I'd really appreciate an explanation. Thanks. – Paolo Mar 13 '11 at 01:57
  • add_to_class requires two arguments, first the name of the model field, then the model field instance: cls.add_to_class('myfield', models.TextField()). The order of register_extensions and register_templates should not matter. – Matthias Kestenholz Mar 13 '11 at 12:49