On Sep 9, 2017 8:59 AM, "Derek" <gamesb...@gmail.com> wrote:

"If you dump data directly in to models or the database, then you risk
dumping in data that hasn't been evaluated by your application which may
produce subtle bugs of the worst kind where things work in dev and break in
prod because your input channel is different."

If this is happening its because you have poor validation on your model
clean(s). You should NEVER just rely on a form to check data for you (at
least beyond very basic sanitation); that logic properly belongs with the
model.  We import direct from spreadsheets to models (note - NOT direct to
the database) because we make use of such logic.


That's not what I'm saying. Validation at the model level should apply to
all instances of the model in any situation. Validation at the form level
can be much more context specific. The two levels of validation are not
mutually exclusive, rather they are complementary.

I'm not saying that there shouldn't be model validation. Quite the
opposite, there absolutely should be. I'm saying that the import of the
data should follow the same workflow as an end-user would enter it to
ensure the data appears correct in the context that it is being entered
where possible.

I'm simple cases, a model is only implemented in a single context (in only
one spot in the application and only via one form). Model and form
validation would usually be the same. In advanced cases, the same model can
be referenced via multiple pages, and each page may place a different set
of restrictions on what data may be entered or how it is formatted. This is
typical when a model is related to several other models.

Hypothetically, if you have a Widget model, it may have a long list of
available colors to choose from in a choices= list. The model validation
would be responsible for ensuring that the color selected is one of the
available colors in the entire list of colors available.

However, Widgets may only be available in red and blue when paired with a
FooBar model. The form for FooBarWidgets would then limit the color choices
to just red and blue, and would validate that the color submitted is one of
those. The model validation wouldn't complain, because red and blue are in
the long list of colors available.

Importing the data directly to the model could potentially mean that a
Widget paired with a FooBar can be set to green, which is invalid. The
model validation wouldn't help in this case, because green is technically a
valid color for widgets, but in the context of FooBars, it isn't.

That's why I mentioned that you need to be 150% confident in your data
generation when importing via the ORM directly to avoid "impossible" data
in your models, which is worse than missing data IMO.

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXetGK9EhQEFyE05g18g8x7gEESRN9vmPw_yNjicC8HbA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to