Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding Fields in Django Class Based Views Not Working

Boy, it feels like I've tried EVERYTHING here, and I just can't get this form to render properly (e.g. with a subset of fields). Here's the relevant items (extra items removed):

models.py:

class Response(models.Model):
    public = models.BooleanField(default=False)
    question = models.CharField(max_length=255, default='', blank=True)

class ResponseForm(ModelForm):
    class Meta:
        model = Response
        fields = ['question']

views.py:

class ResponseCreate(CreateView):
    model = Response
    fields = ['question']

response_form.html:

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Create" />
</form>

I've read every bit of docs, and they suggest using exclude, excludes, field, fields in permutations of tuple, or list, and no matter what the "public" field seems to show up every time! Thoughts?

Thanks for your help!

like image 449
aronchick Avatar asked Jun 18 '13 00:06

aronchick


1 Answers

It looks like you need to specify form_class on your view:

class ResponseCreate(CreateView):
    model = Response
    form_class = ResponseForm
like image 193
Scott Woodall Avatar answered Sep 18 '22 18:09

Scott Woodall