Displaying model in a template.

88 views
Skip to first unread message

Solomon Mbak

unread,
Aug 30, 2019, 7:55:34 PM8/30/19
to Django developers (Contributions to Django itself)
I have a code that looks like this:

class Courses(models.Model):
CourseName = models.CharField(max_length = 250)
CourseDescription = models.CharField(max_length = 250)



class Questions(models.Model):
CAT_CHOICES = models.ForeignKey("main.course_category", on_delete=models.CASCADE)
question = models.CharField(max_length = 250)
optiona = models.CharField(max_length = 100)
optionb = models.CharField(max_length = 100)
optionc = models.CharField(max_length = 100)
answer = models.CharField(max_length = 100)
catagory = models.CharField(max_length=20, choices = CAT_CHOICES)


The work well. The issue is with the views.py (I suspect).

def home(request):
choices = Questions.CAT_CHOICES
print(choices)
return render(request,
'quiz/home.html',
{'choices':choices})



ERRORS:
quiz.Questions.catagory: (fields.E004) 'choices' must be an iterable (e.g., a list or tuple).


What I want is to display the course names on the home.html.

Any help please.

Adam Johnson

unread,
Aug 31, 2019, 4:59:19 AM8/31/19
to django-d...@googlegroups.com
Hi!

I think you've found the wrong mailing list for this post. This mailing list is for the development of Django itself, not for support using Django. This means the discussions of bugs and features in Django itself, rather than in your code using it. People on this list are unlikely to answer your support query with their limited time and energy. Read more on the mailing lists at https://www.djangoproject.com/community/

For support, please use the django-users mailing list, or IRC #django on Freenode, or a site like Stack Overflow. There are people out there willing to help on those channels, but they might not respond if you don't ask your question well. Stack Overflow's question guide can help you frame it well: https://stackoverflow.com/help/how-to-ask .

Also if you haven't read it, please take a look at Django's Code of Conduct: https://www.djangoproject.com/conduct/ . These are our "ground rules" for working well as a community, and will help you get the most out of Django and our fantastic community.

In response to your question - the error message is telling you that the argument "choices" for your category field should not be a foreignkey, but a list - see the documentation and an example in https://docs.djangoproject.com/en/2.2/ref/models/fields/#choices  . The problem is thus in your models and not your view. The Django tutorial uses Question and Choice models and lists them in a template - I think this is similar to what you're trying to do - see https://docs.djangoproject.com/en/2.2/intro/tutorial02/ .

Thanks for your understanding,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/37caabd1-189b-48c4-9893-dc50660debe3%40googlegroups.com.


--
Adam

Bhavesh Kumar

unread,
Aug 31, 2019, 10:05:20 AM8/31/19
to Django developers (Contributions to Django itself)
Hello,


You must be aware of that choice should be a list or tuple in django model. In tuple it could be represented as (table_value, rendered_value) ,like [('c', 'cat'), ('d', 'dog')]

And I see you have initiated it with a foreign key.

Could you please show the values it is having ?


Regards,
Bhavesh Kumar
Reply all
Reply to author
Forward
0 new messages