twoscoops / two-scoops-of-django-1.11

The issue tracker, changelog, and code repository for Two Scoops of Django 1.11

Home Page:https://www.twoscoopspress.com/products/two-scoops-of-django-1-11

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

netvigator opened this issue · comments

Location within the Book page 150

  • Chapter or Appendix: chapter 11
  • Section: Pattern 2: Custom Form Field Validators in ModelForms
  • Subsection: Example 11.5: Adding Custom Validators to a Model Form

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.

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.

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!

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

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.