twoscoops / two-scoops-of-django-1.11

The issue tracker, changelog, and code repository for Two Scoops of Django 1.11
https://www.twoscoopspress.com/products/two-scoops-of-django-1-11
409 stars 68 forks source link

while putting custom validator in form, in form Meta, django required fields statement #124

Open netvigator opened 6 years ago

netvigator commented 6 years ago

Location within the Book page 150

Description

I followed your code for my model named 'Brand', but kept getting an error from Django: django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form BrandForm needs updating.

I finally got pass the error by moving my fields list from the view to the form Class Meta.

So based on my experience, I think your Example 11.5 needs one additional line at the bottom, such as:

fields = "__all__"

Or substitute an actual fields list.

pydanny commented 6 years ago

No, that opens a potential security hole, which we cover extensively in the security chapter.

Unfortunately, I can't tell you how to fix your problem without adding that security hole unless source code is posted.

netvigator commented 6 years ago

OK, I read went through the security chapter again (can't hurt).

Sorry to mention Meta.fields = "__all__". I found that in stackoverflow, which let me to implementing the fix that got me past the error:

I finally got [past] the error by moving my fields list from the view to the form Class Meta.

Still, following the code in the book, I got an error, which I got past by adding an explicit fields list to the Meta.

Am I missing anything? Does adding a fields list to the Meta open any security hole?

Thanks!

pydanny commented 6 years ago

Without seeing the code throwing the error, or even the stack trace of the error, there's nothing I can do.

netvigator commented 6 years ago

I got past the error -- that problem is solved -- my code is working

I had copied verbatim the code in "Example 11.5: Adding Custom Validators to a Model Form" for my model named Brand. That is the code that threw the error.

What you might want to do is determine whether in general the code in the example needs a fields list to work, and if so, in future versions of the book, add a fields list to the example code.

Sorry for the trouble! Bottom line is the book has been extremely helpful for me! I appreciate your help.