Skip to main content
 首页 » 编程设计

python之Django:找不到字段

2024年08月25日27shanyou

一直报错

ImproperlyConfigured at /messages/compose/ 
Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form ComposeForm needs updating. 

我完全理解错误指出的是什么,但是我不明白为什么它会首先出现?

这是 django 要我更新的表单:

from django import forms 
from .models import DirectMessage 
 
class ComposeForm(forms.ModelForm): 
    class Meta: 
        model = DirectMessage 

这是我的模型(包含字段):

from django.db import models 
from django.contrib.auth.models import User 
 
# Create your models here. 
user_obj = User.objects.get(username = 'jess') 
 
class DirectMessage(models.Model): 
    subject = models.CharField(max_length =150) 
    body = models.CharField(max_length =3000) 
    sender = models.ForeignKey(User, related_name='sent_direct_messages', null=True, blank=True) 
    receiver = models.ForeignKey(User, related_name='recieved_direct_messages', null=True, blank=True) 
    sent = models.DateTimeField(auto_now_add=False, auto_now=False, null=True, blank=True) 
    read = models.DateTimeField(auto_now_add=False, auto_now=False, null=True, blank=True) 
 
    def __unicode__(self): 
        return self.subject  

也许我的语法有问题,或者我遗漏了一个明显的基本错误。任何帮助将不胜感激,如果您需要更多信息/上下文,请告诉我。谢谢!

请您参考如下方法:

I totally understand what the error is pointing out however I don't understand why it's showing up in the first place?

错误...因为您的 ModelForm 没有明确指定 fields 列表或 exclude 列表?

Here is the form django wants me to update: class DirectMessageAdmin(admin.ModelAdmin):

这不是 ModelForm,这是 ModelAdmin。鉴于您的错误消息中的 url,我认为它与您的管理员没有任何关系...