2

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 .

2 Answers 2

3

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, e.g. Problem and TestCase instead of problem and testcases.

Sign up to request clarification or add additional context in comments.

1 Comment

aah !! i had myself replaced objects by object when error was there earlier and when and after removing it that replacement was showing error !! anyways thankyou :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.