Python Django错误(属性):&x27;查菲尔德';对象没有属性';与"u"相关';
在我的新项目中,我试图对每个用户进行描述。但我在尝试进行迁移时出错。我不知道如何修理它。 我尝试过不同的方法,但都没有效果,我的代码可能很糟糕,但我对python和django也很陌生 错误:Python Django错误(属性):&x27;查菲尔德';对象没有属性';与"u"相关';,python,django,forms,attributeerror,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 <
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'
我希望有人能帮助我,因为这真的让我很紧张。你把表格和模型混在一起了。模型不指定(HTML)表单,它指定数据库应如何存储数据,因此需要使用模型。CharField
:
class Desc(models.Model):
description = models.CharField(max_length=250)
class Desc(models.Model):
description=models.CharField(最大长度=250)
这样的CharField
没有widget
分配给它,这是您应该在表单级别处理的事情
您可能需要进行迁移,因为到目前为止,您的Desc
模型中没有description
字段
我在某种程度上同意,表单经常有一个同名的字段(这些字段通常是同名模型字段的默认表单字段)是令人困惑的。不过,我们的想法是模型字段指定数据库中的列,而表单字段指定(HTML)表单中的文本框、复选框等。也许您应该使用
模型。CharField
而不是表单。CharField
?谢谢,这帮了大忙:D:D