python - 属性错误 : (Class) object has no attribute '__name__' Creating ModelForms [Django & Python2. 7]

标签 python django postgresql python-2.7 modelform

这是我第一次使用 Django,我完全不知道如何在我的项目中使用 ModelForms。到目前为止,我已经能够按照在线教程进行操作,但是没有 ModelForms(将数据添加到 Postgresql 数据库),我无法继续。我试图简单地制作一个表单页面,让用户添加一些输入(2 个日期字段和 1 个文本字段),并通过提交该表单,将数据添加到数据库中。

我得到的错误是: AttributeError: 'Hyuga_Requests' 对象没有属性 'name' [其中 Hyuga_Request 是在 models.py 中设置的类]

模型.py

from __future__ import unicode_literals
from django.db import models
from django.forms import ModelForm

class Hyuga_Requests(models.Model):
    name = models.CharField(max_length=50)
    s_date = models.DateField(auto_now=True)
    e_date = models.DateField(auto_now=True)
    reason = models.TextField(max_length=500)

def __unicode__(self):
    return self.name

View .py

from django.shortcuts import render
from django import forms
from .forms import Hyuga_RequestForm

def create_req(request):
    form = Hyuga_RequestForm()
    context = {"form":form,}
    return render(request,"request_form/requestform.html", context)

表单.py

from django import forms
from .models import Hyuga_Requests
from django.forms import ModelForm

class Hyuga_RequestForm(forms.ModelForm):
    class Meta:
        model = Hyuga_Requests()
        fields = ['name','s_date','e_date','reason']

请帮助这个菜鸟...

最佳答案

不要在 Hyuga_RequestForm 类的 Meta 类中实例化模型。

model = Hyuga_Requests() 应该是 model = Hyuga_Requests

关于python - 属性错误 : (Class) object has no attribute '__name__' Creating ModelForms [Django & Python2. 7],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44014250/

相关文章:

python - 在 PyQt4 中连接多个按钮信号时如何避免这种后期绑定(bind)的情况?

postgresql - PostgreSQL 中的 PL/pgSQL 查询返回新的空表的结果

sql - 为什么这个 n-n 关系中的查询什么都不返回?

python - Django 无法在 PostgreSQL 上创建 super 用户。 "server closed the connection unexpectedly"

python - Odoo Studio 服务器错误计算字段,错误

python - Django & Celery 使用单例类实例属性作为全局

python - pd.date_range如何排除几个小时

html - Django - 自定义表单,特别是字段的外观

python - 是否有必要调整 Django 应用程序中的 settings.py 以正确设置 SSL 保护(使用 nginx 反向代理 + gunicorn)

python - 使用 SSL 使用 Daphne + NGINX 部署 Django Channels