illevens
illevens

Reputation: 373

AttributeError: module 'django.db.models' has no attribute 'RichTextField'

I'm new to wagtail and am following both official and youtube tutorials (yt ones skip instructions to installing libjpeg and zlib but when I tried to, it says Pillow has them included); Running on windows10, Using latest python, latest wagtail, latest django; Stuck on the first spot, still getting the same issue when running makemigrations or migrate or runserver:

File "C:\Users\TalkyWalky\Desktop\Workspace\bloggyblog\home\models.py", line 14, in 
HomePage
    banner_subtitle = models.RichTextField(features=["bold", "italic"])
AttributeError: module 'django.db.models' has no attribute 'RichTextField'

here's my models.py:

    from django.db import models

from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel

class HomePage(Page):
    """Homepage model."""
    body = RichTextField(blank=True)
    templates = "templates/home/home_page.html"
    max_count = 1

    banner_title = models.CharField('Body', blank=True)
    banner_subtitle = models.RichTextField(features=["bold", "italic"])
    banner_image = models.ForeignKey(features=
        "wagtailImages.Image",
        null=False,
        blank=False,
        on_delete=models.SET_NULL,
        related_name="+"
    )
    banner_cta = models.ForeignKey(
        "wagtailcore.Page",
    )

    content_panels = Page.content_panels + [
        FieldPanel("banner_title"),

    ]
    class Meta:
        verbose_name = "HomePage"
        verbose_name_plural = "HomePages"

And here's full traceback :

Traceback (most recent call last):
  File ".\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\TalkyWalky\Desktop\Workspace\bloggyblog\venv_bloggyblog\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line      
    utility.execute()
  File "C:\Users\TalkyWalky\Desktop\Workspace\bloggyblog\venv_bloggyblog\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "C:\Users\TalkyWalky\Desktop\Workspace\bloggyblog\venv_bloggyblog\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\TalkyWalky\Desktop\Workspace\bloggyblog\venv_bloggyblog\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\TalkyWalky\Desktop\Workspace\bloggyblog\venv_bloggyblog\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Program Files\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed        
  File "C:\Users\TalkyWalky\Desktop\Workspace\bloggyblog\home\models.py", line 7, in <module>
    class HomePage(Page):
  File "C:\Users\TalkyWalky\Desktop\Workspace\bloggyblog\home\models.py", line 14, in 
HomePage
    banner_subtitle = models.RichTextField(features=["bold", "italic"])
AttributeError: module 'django.db.models' has no attribute 'RichTextField'

And here's what happens when I run wagtail updatemodulepaths:

Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python38\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\TalkyWalky\Desktop\Workspace\bloggyblog\venv_bloggyblog\Scripts\wagtail.exe\__main__.py", line 7, in <module>
  File "c:\users\talkywalky\desktop\workspace\bloggyblog\venv_bloggyblog\lib\site-packages\wagtail\bin\wagtail.py", line 297, in main
    command.execute(sys.argv)
  File "c:\users\talkywalky\desktop\workspace\bloggyblog\venv_bloggyblog\lib\site-packages\wagtail\bin\wagtail.py", line 52, in execute
    self.run(**options_dict)
  File "c:\users\talkywalky\desktop\workspace\bloggyblog\venv_bloggyblog\lib\site-packages\wagtail\bin\wagtail.py", line 171, in run
    change_count = self._rewrite_file(path)
  File "c:\users\talkywalky\desktop\workspace\bloggyblog\venv_bloggyblog\lib\site-packages\wagtail\bin\wagtail.py", line 236, in _rewrite_file
    for original_line in f:
  File "C:\Program Files\Python38\lib\fileinput.py", line 248, in __next__
    line = self._readline()
  File "C:\Program Files\Python38\lib\encodings\cp1252.py", line 23, in decode        
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 641: character 
maps to <undefined>

Upvotes: 4

Views: 1516

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477598

You should not write models.RichTextField, since indeed, such class does not exists in the django.db.models module. You should simply use RichTextField, since you imported that one:

from django.db import models

from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel

class HomePage(Page):
    """Homepage model."""
    body = RichTextField(blank=True)
    templates = "templates/home/home_page.html"
    max_count = 1

    banner_title = models.CharField('Body', blank=True)
    # no models.
    banner_subtitle = RichTextField(features=["bold", "italic"])
    banner_image = models.ForeignKey(features=
        "wagtailImages.Image",
        null=False,
        blank=False,
        on_delete=models.SET_NULL,
        related_name="+"
    )
    banner_cta = models.ForeignKey(
        "wagtailcore.Page",
    )

    content_panels = Page.content_panels + [
        FieldPanel("banner_title"),

    ]
    class Meta:
        verbose_name = "HomePage"
        verbose_name_plural = "HomePages"

Upvotes: 6

Related Questions