Thank you. There are way more parts to this than I would have imagined.
On Sat, Jul 13, 2019, 8:01 PM Mike Dewhirst <mi...@dewhirst.com.au> wrote: > On 14/07/2019 10:37 am, Dean Karres wrote: > > Hi, > > I am learning Django. I am using CBVs. My default "index.py" view is > basically a dashboard for the app I am playing with. As my models and > views become more complicated I want to be able to ask, for any model > instance, is this instance "valid" or "complete". Valid means that all > Required elements are present and have reasonable values. "Complete" means > that an instance is "valid" but also some specific bits of additional info > are also ok. > > For example I have a Student model that has name and address info. There > is a ManyToMany relation to the Class(es) in which that Student is > enrolled. A "student" instance is valid if the name and address fields are > filled. A student is "complete" if the student is valid and has signed up > for one or more classes. > > So, my question is: where should the valid and complete methods live? > Should they be in the Student model or CBV? Someplace else? Does it > matter? > > > I like to put that sort of stuff into model methods then add model.clean() > to call them and raise whatever error may be appropriate if they fail. > > > https://docs.djangoproject.com/en/2.1/topics/forms/modelforms/#interaction-with-model-validation > > and > > > https://docs.djangoproject.com/en/2.1/ref/models/instances/#validating-objects > > and > > > https://docs.djangoproject.com/en/2.1/ref/models/instances/#django.db.models.Model.clean > > You can raise your own errors which inherit from ValidationError to fine > tune the process. For example, I differentiate between > BusinessRuleViolation and InvalidToken and a couple of others where that > delivers better control. > > > > Cheers > -- > 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/22801669-96ec-4a43-a264-fd50c2544604%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/22801669-96ec-4a43-a264-fd50c2544604%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+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/CAO%2B1Q_uv_PN%3Dm8Ny5zWyQVGbSkY2mnwZJO-dYQy2yL8CbOTgsw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.