Django add word count in model

Sometimes it is necessary to find out which posts are not long enough to stay on website. Needs to be expanded to make it better. This way you can add a count, so it is easy sort by. 

     def save(self, *args, **kwargs):
        self.words_count = len(self.raw_text.split())
        super(StaticPage, self).save(*args, **kwargs)


root@Ubuntu-2010-groovy-64-minimal /home/sites/auto-verkopen-belgie/djangopageadmin # python3 manage.py makemigrations page
Migrations for 'page':
  page/migrations/0008_staticpage_words_count.py
    - Add field words_count to staticpage
root@Ubuntu-2010-groovy-64-minimal /home/sites/auto-verkopen-belgie/djangopageadmin # python3 manage.py migrate page
Operations to perform:
  Apply all migrations: page
Running migrations:
  Applying page.0008_staticpage_words_count... OK
root@Ubuntu-2010-groovy-64-minimal /home/sites/auto-verkopen-belgie/djangopageadmin # python3 manage.py shell
Python 3.8.6 (default, Jan 27 2021, 15:42:20)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from page.models import *
>>> dir()
['DubbeleContent', 'Img', 'NOW', 'PageOption', 'StaticPage', '__builtins__', 'choices', 'datetime', 'escape', 'mark_safe', 'models', 'ngettext', 'now', 'reverse', 'settings']
>>> pages = StaticPage.objects.all()
>>> for page in pages:
...  page.save()
...

>>>
>>> exit()



Comments