AttributeError:type object' Team'没有属性' _meta'

时间:2016-02-13 23:40:35

标签: python django sqlite django-models django-forms

所以我对Django来说是个新手,对我来说,生活似乎无法弄清楚这里发生了什么。我有一个表单正在工作并显示在一个网页中,我能够创建一个数据库并在SQL中显示。 但是,当我尝试将表单保存到数据库中时,我开始测试的字段已经消失,没有任何内容写入数据库。

我也一直收到此错误:AttributeError: type object 'Team' has no attribute '_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'

models.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'

views.py

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, })

forms.py

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>

2 个答案:

答案 0 :(得分:1)

问题出在forms.py。看到你有这个模型导入:

from .models import Team

然后您定义Team表单阴影导入的模型:

class Team(forms.Form):

然后,当您在model = Team中使用TeamForm时,它实际上会使用Team表单引用,而不是导入的模型。

解决这个问题的一种方法是为您的import语句添加别名:

from .models import Team as TeamModel

然后使用它在模型表单上设置model

class TeamForm(ModelForm):
    class Meta:
        model = TeamModel
        fields = ['team_number', 'team_name']

答案 1 :(得分:0)

如果您刚刚添加了字段集或自定义了您的管理页面,您应该重新启动服务器以将这些更改应用到您的项目中,否则也会导致类似的错误。

文档中没有提到它,因此希望它可以帮助某人。