D
D
Dauren S2016-08-02 15:28:53
Django
Dauren S, 2016-08-02 15:28:53

In Django in forms.py in html to display list of categories from model?

This is the category model

from django.db import models
class Answer_Category(models.Model):
    title = models.CharField(max_length=200)
    url = models.CharField(max_length=200,blank=True, null=True)
    parent = models.ForeignKey('Answer_Category',blank=True, null=True)
    description=models.CharField(max_length=200,blank=True, null=True)
    path=models.CharField(max_length=200,blank=True, null=True)
    metadesc=models.CharField(max_length=200,default='',blank=True, null=True)
    published=models.BooleanField(default=True)
    position=models.IntegerField(default=0)

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        url=slugify(self.title, 'ru')
        self.url = url
        super(Answer_Category, self).save(*args, **kwargs)

This is forms.py
from django import forms
from .models import Answer_Category
class AnswerForm(forms.Form):
    title = forms.CharField(label='Название', max_length=100)
    description = forms.CharField(label='Текст вопроса',widget=forms.Textarea)
    email = forms.CharField(label='Email', max_length=100)

How to add category list from Answer_Category class

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dauren S, 2016-08-03
@dauren101

from django import forms
from .models import Answer_Category
class AnswerForm(forms.Form):
    title = forms.CharField(label='Название', max_length=100)
    description = forms.CharField(label='Текст вопроса',widget=forms.Textarea)
    category = forms.ModelChoiceField(queryset=Answer_Category.objects.all(), empty_label="Выберите значение")
    email = forms.CharField(label='Email', max_length=100)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question