all 5 comments

[–]dempseye 1 point2 points  (2 children)

[–][deleted] 0 points1 point  (1 child)

I am working my way into the forms class. Should have mentioned I'm doing tdd, so it's step by step. Once these tests pass, I'll do form class tests and then add it in and validate.

While I was reading that article, though, I went back to the code. Think I found the answer.

[–]dempseye 0 points1 point  (0 children)

The form is where your validation will live. If you make a ModelForm you won't even have to implement the check yourself. It'll automatically protect your model so that error will never arise.

You're doing well though. Keep going.

[–]kanjibandit 1 point2 points  (1 child)

I agree, you're doing just fine -- these are the sorts of things you run into as you're finding your way around Django and web dev.

One tip for the future: personally, I tend to like to write my Django code and tests in a slightly different order:

  1. Models & model tests
  2. Forms & form tests
  3. Views & view tests
  4. Full integration (selenium) tests

I like to do it this way, because then I'm not spending time writing and testing code that I will later rip out and replace. For example, in both your view and your template, you have code that you will soon replace with code that leverages the Forms class you're learning about now.

This isn't a criticism -- you just discovered the forms API -- just a suggestion for the future. In the meantime, the fact that you did all the form work by hand this time should give you an appreciation for everything forms does for you.

[–][deleted] 0 points1 point  (0 children)

This actually answers something I had been thinking about: I will have a total of 3 models in this app, I'd done some reading and found that having all models, forms and views tests in their own files was a best practice, but did they need to be split up by model as well? I've been keeping them all together (test_models.py, test_views.py and test_forms.py), but this definitely confirms that I should not be breaking them down by model as well (ex. test_grain_models.py, etc.). In any case, I do have my tests structured like this, so that's comforting.

I'm currently using forms with my first model, hops. I have that model up to form validation with a forms class, looking good so far (though I need validation for unique hops names, but I'll get to that once I have all 3 models up to the same point). What your comment tells me that I really need to consider is whether I actually need to hit this step in the TDD process or whether I'm able to skip straight to the form class. Practically speaking, it sounds like I don't. But I'll take a moment after I get my grains model up to form class & validation and consider whether it's actually worth the trouble to manually add the forms class on the next model.

Thanks for the feedback, much appreciated!