简体   繁体   中英

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

forms.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.

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 .

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.

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 .

You just mistype, in your get_list function it should be objects not object .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM