Python AttributeError:type object';团队';没有属性'_meta&x27;
所以我对Django还是一个新手,我一辈子都不知道这里发生了什么。我有一个表单工作并显示在一个网页上,我能够创建一个数据库并显示在SQL中。 然而,当我试图获取表单以将信息保存到数据库中时,我开始测试的字段已经消失,没有任何内容写入数据库 我还经常遇到这样的错误:Python AttributeError:type object';团队';没有属性'_meta&x27;,python,django,sqlite,django-models,django-forms,Python,Django,Sqlite,Django Models,Django Forms,所以我对Django还是一个新手,我一辈子都不知道这里发生了什么。我有一个表单工作并显示在一个网页上,我能够创建一个数据库并显示在SQL中。 然而,当我试图获取表单以将信息保存到数据库中时,我开始测试的字段已经消失,没有任何内容写入数据库 我还经常遇到这样的错误:AttributeError:type对象“Team”没有属性“\u meta” 回溯: Unhandled exception in thread started by <function wrapper at 0x108cbd
AttributeError:type对象“Team”没有属性“\u meta”
回溯:
Unhandled exception in thread started by <function wrapper at 0x108cbd050>
Traceback (most recent call last):
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/checks/urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/checks/urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/alicen/git/first_robotics/frcstats/urls.py", line 18, in <module>
from .views import get_name # , post_new
File "/Users/alicen/git/first_robotics/frcstats/views.py", line 4, in <module>
from .forms import *
File "/Users/alicen/git/first_robotics/frcstats/forms.py", line 14, in <module>
class TeamForm(ModelForm):
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/forms/models.py", line 247, in __new__
opts.field_classes)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/forms/models.py", line 144, in fields_for_model
opts = model._meta
AttributeError: type object 'Team' has no attribute '_meta'
视图.py
from django.db import models
class Team(models.Model):
team_number = models.IntegerField()
team_name = models.CharField(max_length=30)
robot_weight = models.FloatField()
robot_height = models.IntegerField()
team_location = models.CharField(max_length=30)
team_notes = models.CharField(max_length=150)
posted_on = models.DateTimeField('Posted On')
def __unicode__(self):
return self.team_number
class Meta:
db_table = 'teams'
app_label = 'frcstats'
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import *
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
team = Team(request.POST)
match = Match(request.POST)
auto = Autonomous(request.POST)
teleop = Teleoperated(request.POST)
# check whether it's valid:
if form.is_valid() and auto.is_valid() and match.is_valid() and teleop.is_valid():
form.save()
return HttpResponseRedirect(reverse('frcstats:url'))
else:
return HttpResponseRedirect('/Form not valid/')
# if a GET (or any other method) we'll create a blank form
else:
team = Team()
auto = Autonomous()
match = Match()
teleop = Teleoperated()
return render(request, 'name.html', {'team': team, 'auto': auto,
'match': match, 'teleop': teleop, })
from django import forms
from .models import Team
from django.forms import ModelForm
class Team(forms.Form):
team_number = forms.IntegerField(label='Team Number ')
team_name = forms.CharField(label='Team Name ', max_length=30)
class TeamForm(ModelForm):
class Meta:
model = Team
fields = ['team_number', 'team_name']
class Match(forms.Form):
match_playing = forms.IntegerField(label='Match Number ')
forms.py
from django.db import models
class Team(models.Model):
team_number = models.IntegerField()
team_name = models.CharField(max_length=30)
robot_weight = models.FloatField()
robot_height = models.IntegerField()
team_location = models.CharField(max_length=30)
team_notes = models.CharField(max_length=150)
posted_on = models.DateTimeField('Posted On')
def __unicode__(self):
return self.team_number
class Meta:
db_table = 'teams'
app_label = 'frcstats'
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import *
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
team = Team(request.POST)
match = Match(request.POST)
auto = Autonomous(request.POST)
teleop = Teleoperated(request.POST)
# check whether it's valid:
if form.is_valid() and auto.is_valid() and match.is_valid() and teleop.is_valid():
form.save()
return HttpResponseRedirect(reverse('frcstats:url'))
else:
return HttpResponseRedirect('/Form not valid/')
# if a GET (or any other method) we'll create a blank form
else:
team = Team()
auto = Autonomous()
match = Match()
teleop = Teleoperated()
return render(request, 'name.html', {'team': team, 'auto': auto,
'match': match, 'teleop': teleop, })
from django import forms
from .models import Team
from django.forms import ModelForm
class Team(forms.Form):
team_number = forms.IntegerField(label='Team Number ')
team_name = forms.CharField(label='Team Name ', max_length=30)
class TeamForm(ModelForm):
class Meta:
model = Team
fields = ['team_number', 'team_name']
class Match(forms.Form):
match_playing = forms.IntegerField(label='Match Number ')
name.html
<form action="/your-name/add/" method="post">
{% csrf_token %}
<h1>Match Scouting Form</h1>
{{ team.as_p }}
{{ match.as_p }}
<h3>Autonomous</h3>
{{ auto.as_p}}
<h3>Teleoperated</h3>
{{ teleop.as_p}}
<input type="submit" value="Submit" />
</form>
{%csrf_令牌%}
比赛球探表格
{{team.as_p}}
{{match.as_p}}
自主的
{{auto.as_p}}
遥控操作
{{teleop.as_p}}
问题出在表单.py中。请查看您是否有此模型导入:
from .models import Team
然后定义对导入模型进行阴影处理的团队
表单:
class Team(forms.Form):
然后,当您在TeamForm
中使用model=Team
时,它实际上将使用Team
表单引用,而不是导入的模型
解决此问题的一种方法是将导入语句别名为:
from .models import Team as TeamModel
然后使用它在模型窗体上设置模型
:
class TeamForm(ModelForm):
class Meta:
model = TeamModel
fields = ['team_number', 'team_name']
如果您刚刚添加了字段集或自定义了管理页面,则应重新启动服务器,以便将这些更改添加到项目中,否则也会导致类似的错误
文档中没有提到这一点,因此希望它能帮助他人。谢谢!这解决了我的属性错误,现在我要弄清楚如何让它写入我的数据库