AttributeError:“函数”对象没有属性“ _meta”

时间:2018-12-26 21:44:34

标签: python django

当我在我的views.py中添加行from .forms import Opret_kunde_form时,标题出现了错误,这使我发疯。我不知道该如何解决。

完整追溯

> Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001E23A239400>
Traceback (most recent call last):
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 396, in check
    for pattern in self.url_patterns:
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\kr85m\Google Drive\gitlab\DeathToPlexus\DeathToPlexus\DeathToPlexus\urls.py", line 25, in <module>
    path('management/new/customer', include('createCustomer.urls')),
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\kr85m\Google Drive\gitlab\DeathToPlexus\DeathToPlexus\createCustomer\urls.py", line 3, in <module>
    from . import views
  File "C:\Users\kr85m\Google Drive\gitlab\DeathToPlexus\DeathToPlexus\createCustomer\views.py", line 2, in <module>
    from .forms import Opret_kunde_form
  File "C:\Users\kr85m\Google Drive\gitlab\DeathToPlexus\DeathToPlexus\createCustomer\forms.py", line 7, in <module>
    class Opret_kunde_form(ModelForm):
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\models.py", line 256, in __new__
    apply_limit_choices_to=False,
  File "C:\Users\kr85m\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\models.py", line 139, in fields_for_model
    opts = model._meta
AttributeError: 'function' object has no attribute '_meta'

Models.py

from django.db import models
# Create your models here.


def Opretkunde():
    Fornavn = models.CharField(max_length=30)
    Efternavn = models.CharField(max_length=50)
    Telefon = models.IntegerField(max_length=15)
    Adresse = models.CharField(max_length=50)
    Postnummer = models.IntegerField(max_length=4)
    IA = models.CharField(max_length=10)

forms.py

from django import forms
from .models import Opretkunde
from django.forms import ModelForm



class Opret_kunde_form(ModelForm):
    class Meta:
        model = Opretkunde
        fields = ['Fornavn', 'Efternavn', 'Telefon', 'Adresse']

Views.py

from django.shortcuts import render
from .forms import Opret_kunde_form
# Create your views here.
def index(request):
    if request.method == 'POST': #Påbegynder starten af scriptet hvis der observeres en POST request
        form = Opret_kunde_form(request.POST, request.FILES) #initialiserer formen
        if form.is_valid():
            return render(request, 'createUser.html',)

1 个答案:

答案 0 :(得分:4)

Django模型的子类models.Model本身必须是一个类,因为该类的实例将是模型实例。

class Opretkunde(models.Model)
    # the rest of your model fields go here

这就是为什么出现此错误的原因,函数没有属性_meta,因为函数不能具有元类,而只能具有类。