Django错误(属性):"CharField"对象没有属性"is_related" [英] Django Error (Attribute): 'CharField' object has no attribute 'is_related'

查看:256
本文介绍了Django错误(属性):"CharField"对象没有属性"is_related"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a description to every user, in my new project. But i get an error when i try to makemigrations. I do not know how to fix it. I have tried different things but nothing worked, my coding is maybe very bad, but i am also new to python and 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'

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

I Hope somebody can help me, because this is really getting on my nerves.

推荐答案

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

You mixed forms and models. A model does not specify a (HTML) form, it specifies how the database should store data, so you need to use a models.CharField:

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

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

Such CharField has no widget assigned to it, this is something you should handle at the form level.

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

You probably will need to make migrations, since up to this point, there was no description field in your Desc model.

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

I agree to some extent that it is confusing that the forms have frequently a field with the same name (well those typically are the default form fields for the model field with the same name). The idea is however that model fields specify the columns in a database, whereas form fields specify text boxes, check boxes, etc. in a (HTML) form.

这篇关于Django错误(属性):"CharField"对象没有属性"is_related"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆