简体   繁体   English

Python / DJango属性错误:模型对象没有属性对象

[英]Python/DJango Attribute error : Model object has not attribute objects

forms.py : form.py:

from django.db import models
from django import forms
from pset.models import problem , testcases 

class problems(forms.ModelForm):
    class Meta:
        model=problem
        fields=['pcode','pdesc']

class testcases(forms.ModelForm):
    class Meta:
        model=testcases
        fields=['pcode','inp','out']

    def __init__(self,*args,**kwargs):
        super(testcases,self).__init__(*args,**kwargs)
        self.fields['pcode']=forms.ChoiceField(choices=get_list())


def get_list() :
    tup=((x,x) for x in problem.object.values_list('pcode',flat=True))
    return tup

here are two modelforms one is problems and other one is testcases . 这是两种模型形式,一种是问题,另一种是测试用例。 I was trying to include a dropdown menu in it. 我试图在其中包含一个下拉菜单。 For it tried to include the pcode column from problem Model. 因为它试图包括问题模型中的pcode列。

But don't know why it is shooting up an error : 但不知道为什么会报错:

AttributeError at /setup/add_cases/ type object 'problem' has no attribute 'object' in the function get_list . / setup / add_cases /类型对象“问题”处的AttributeError在函数get_list中没有属性“ object”

In case required : 如果需要:

Models.py 型号

from django.db import models

# Create your models here. 
class problem(models.Model) :
    pcode=models.CharField(max_length=10,unique=True)
    pdesc=models.TextField() 

    def __str__(self) :
        return self.pcode   

class testcases(models.Model):
    pcode=models.CharField(max_length=10)
    inp=models.FileField(upload_to='testcases',blank=True)
    out=models.FileField(upload_to='testcases',blank=True)

    def __str__(self):
        return self.pcode

apologies if any detail has been left out . 如果遗漏任何细节表示歉意。

It's a typo on this line: 这是错字:

problem.object.values_list('pcode',flat=True))

You are missing the 's' from objects. 您缺少对象中的“ s”。

problem.objects.values_list('pcode',flat=True))

As an aside, the convention is to use CamelCase for your Django models, and have them singular not plural, eg Problem and TestCase instead of problem and testcases . 顺便说一句,惯例是对您的Django模型使用CamelCase,并使其单数形式而不是复数形式,例如ProblemTestCase而不是problemtestcases

You just mistype, in your get_list function it should be objects not object . 您只是get_list ,在get_list函数中它应该是objects而不是object

def get_list() :
    tup=((x,x) for x in problem.objects.values_list('pcode',flat=True))
    return tup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DJANGO自定义用户模型错误“ str”对象没有属性“ objects” - DJANGO custom user model error 'str' object has no attribute 'objects' python django 'QuerySet' object 没有属性 'objects' - python django 'QuerySet' object has no attribute 'objects' 'super' object 没有属性 'objects' Python Django - 'super' object has no attribute 'objects' Python Django “属性错误/功能”object 在 Django 中没有属性“对象” - 'Attribute Error/ function' object has no attribute 'objects' in Django Python类和对象属性错误:对象没有属性 - Python Classes and Objects Attribute error : object has no attribute '函数'对象没有属性'对象'Django 1.8搜索模型 - 'function' object has no attribute 'objects' Django 1.8 searching a model Django 用户 Model AttributeError: 'str' object 没有属性 'objects' - Django User Model AttributeError: 'str' object has no attribute 'objects' 属性错误:“ str”对象没有属性“ read” python-django - Attribute error: 'str' object has no attribute 'read' python-django Django属性错误:对象没有属性'表单' - Django Attribute Error: object has not attribute 'forms' Django 错误:类型 object 'Invoice_Line' 没有属性 'objects' - Django Error: type object 'Invoice_Line' has no attribute 'objects'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM