Django错误(属性):“ CharField”对象没有属性“ is_related”

时间:2018-10-10 07:34:00

标签: python django forms attributeerror

我正在尝试在我的新项目中对每个用户进行描述。但是当我尝试进行移民时,我得到一个错误。我不知道如何解决。 我尝试了不同的方法,但没有任何效果,我的编码可能非常糟糕,但我还是python和django的新手。

错误:

C:\Users\bruger\Dropbox\min-login-web\web_login>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\makemigrations.py", line 143, in handle
    loader.project_state(),
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\loader.py", line 322, in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\graph.py", line 378, in make_state
    project_state = self.nodes[node].mutate_state(project_state, preserve=False)
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\migration.py", line 87, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\operations\models.py", line 85, in state_forwards
    list(self.managers),
  File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\state.py", line 377, in __init__
    if field.is_relation and hasattr(field.related_model, '_meta'):
AttributeError: 'CharField' object has no attribute 'is_relation'

我的模型文件:

from django import forms
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from PIL import Image


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self):
        super().save()

        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

class Desc(models.Model):
    description = forms.CharField(widget = forms.Textarea, max_length = 250, required=False)

    def __str__(self):
        return f'{self.user.username} Desc'

我希望有人能帮助我,因为这真的让我感到不安。

1 个答案:

答案 0 :(得分:1)

您混合了形式模型。模型没有指定(HTML)表单,而是指定数据库应如何存储数据,因此您需要使用models.CharField

class Desc(models.Model):
    description = models.CharField(max_length=250)

此类CharField没有分配给widget,这是您应该在 form 级别处理的。

您可能需要进行迁移,因为到目前为止,description模型中没有Desc字段。

在某种程度上,我同意这些表单经常具有名称相同的字段(这通常是具有相同名称的model字段的默认表单字段)令人困惑。但是,想法是模型字段指定数据库中的列,而表单字段指定(HTML)表单中的文本框,复选框等。