Thanks, yes I did look at model validation in the docs - it looked to be tied to use via forms. I hadn't realised that it's not called automatically when an instance is created. So if I was to validate when using get_or_create method would I then manually call full_clean() after get_or_create() ?
I think you may be right regarding just setting up forms for the models. I don't need a form but do need the validation. Thanks for your help. On Wednesday, 21 October 2015 19:09:01 UTC+1, Simon Charette wrote: > > Hi Yunti, > > Did you read about model level validation > <https://docs.djangoproject.com/en/1.8/ref/models/instances/#validating-objects>? > > Calling model_instance.full_clean() triggers validation but it's not > implicitly called when you save an instance. > > For your date case you'll have to include a layer that feeds Django models > with datetime.date objects from your string representation. That's what > Django forms do under the hood using the DATE_INPUT_FORMAT setting > <https://www.google.com/url?q=https%3A%2F%2Fdocs.djangoproject.com%2Fen%2F1.8%2Fref%2Fsettings%2F%23date-input-formats&sa=D&sntz=1&usg=AFQjCNG918aoHFS_G4IDK9V4zC0StTrS8g> > . > > I would suggest you define Model form for your models and use them to > perform conversion and validation of your scrapped data. > > Simon > > Le mercredi 21 octobre 2015 13:49:08 UTC-4, Yunti a écrit : >> >> I have a django project where I want to save web scraped data to the >> database via the django ORM- which will be used in the django app. >> >> The data is currently in the form of JSON -> converted to python dict via >> json.loads >> >> I setup my model with the narrowest/ most constrained field type possible >> - (e.g. DecimalField with decimal_places=2 and max_digits=4 for prices) >> >> I naively tried to save the data/values from the relevant keys in the >> JSON directly to the relevant model field, however, this raised errors due >> to data format. >> >> It looks like data entered via a form is converted to the relevant python >> object in the form validation - e.g. a date string '24 May 2015' is >> converted to a datetime object and the date format validated. >> >> None of this appears to happen when saving directly to a model? (would be >> good to have my understanding here confirmed?) and so saving '24 May 2015' >> directly to a DateField in a model produces a format error. >> >> What validation (if any) does Django do when saving to a database >> directly via the ORM? - Does it just rely on the type checking in the >> database (so for sqlite this would be nothing but would for postgres)? >> >> Thanks. >> >> >> -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f8f33490-8dff-4e9f-8230-14c195609e6f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

