[英]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,并使其单数形式而不是复数形式,例如
Problem
和TestCase
而不是problem
和testcases
。
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.