用Django计算TextField字符

时间:2018-10-16 07:36:52

标签: django-models python-3.6 counting django-2.1

我已经使用Python3.6创建了this simple counter,并且希望对模型中的TextField做同样的事情。

我感兴趣的是第37行,here;我想在Django中运输它。

class Post(models.Model):
    title = models.CharField(max_length=70, help_text="Write post title here. The title must be have max 70 characters", verbose_name="Titolo", unique=True)
    slug = models.SlugField(verbose_name="Slug", unique="True", help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents")
    tagline = models.TextField(max_length=200, help_text="Write a post short description here. The description must be have max 200 characters", verbose_name="Breve descrizione dell'articolo")
    contents = models.TextField(help_text="Write your post here", verbose_name="Contenuti")
    time_of_reading = models.IntegerField(count(contents) / 250)
    highlighted = models.BooleanField(default=False, help_text="If you want that the post went be highlighted, click on this area", verbose_name="Articolo in evidenza")

    def __str__(self):
        return self.title

有了这个我有这个错误:

  File "/var/www/html/dev/miosito/django/v2.1/versions/aproject/muletto/models.py", line 4, in <module>
    class Post(models.Model):
  File "/var/www/html/dev/miosito/django/v2.1/versions/aproject/muletto/models.py", line 10, in Post
    time_of_reading = models.IntegerField(count(contents) / 250)

NameError: name 'count' is not defined

我是开发领域的新手

1 个答案:

答案 0 :(得分:1)

一种可能性是重写方法save()。 在save()中执行以下操作:

    self.thistime_of_reading = self.count(self.contents)

此处: Django. Override save for model