Wednesday, July 26, 2017

Re: How can I auto-fill a field in the model without showing a form field to the user?

On 27/07/2017 9:18 AM, Alexander Joseph wrote:
> Thanks I'll pick up that book.
>
> Would another acceptable approach be to do this in the view by
> overriding the form_valid() method? Can you tell me if there is any
> benefit/disadvantage to doing it one way or another? Thanks again

If you can do it in the model that is where it should be done.

I could be wrong but I believe fundamental business rules should be
built into the model as well.

So I override the model.save() method to do the auto-fill and override
the model.clean() method for business rules. The model clean() method
gets called by form.is_valid() automatically on saving in the browser
and can raise an exception if for example some perfectly valid data
violates a business rule.

https://docs.djangoproject.com/en/1.10/ref/models/instances/#validating-objects

Consider that down the track you wish to make a phone app with access to
your database. The only reasonable way to do that is via the model API.
That means you have to do the work in your phone app that you did in
your web form. It is likely easier to just call the model.full_clean()
method before model.save() in the API.

>
>
>
>
>
> On Tuesday, July 25, 2017 at 4:48:22 PM UTC-6, Mike Dewhirst wrote:
>
> On 26/07/2017 3:15 AM, Alexander Joseph wrote:
> > How did you learn all the methods that are available
>
> Django is very comprehensively documented and almost any question you
> can think up is only a google search away. Typically a search will
> reveal a Django doc reference and StackOverflow answers. I must say
> looking up the docs is very valuable because you learn so much else
> besides your original quest.
>
> Books are another good source. My preferred one is Two Scoops of
> Django.
>
> Welcome.
>
>
> --
> 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+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto: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/5c76493d-11a4-4460-9522-adc2822765e9%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/5c76493d-11a4-4460-9522-adc2822765e9%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@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/27e552a6-3a0e-3a72-d11a-958898346940%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment