On 1 mars 2014, at 09:14, Martin Matusiak <nume...@gmail.com> wrote:You should never, ever, for any reason, use a different database in
> Now in the case of sqlite, which is very permissive, it will happily
> accept values like 1 and {} and store them as "1" and "{}" (wtf)
> respectively. So if I'm running my tests against sqlite I won't even
> know that this will bite me in production.
development, test and production. It's a bad idea. It doesn't work.
An important reason is performance. If creating and saving an instance with
> The thing is that it would be pretty easy to provide a strongly typed
> model layer using descriptors. So that every time I assign to
> bill.name it will raise ValidationError if the value isn't a string,
> if the string is longer than max_length etc. Is there a rationale for
> why we don't do this?
10 fields took 0.01 seconds, that would be poor. For the same reason,
Model.save() doesn't call Model.full_clean() (unless you override it).
A structural reason is that you can't tell "assigning an attribute to an
instance in order to save it to the database later" apart from "assigning an
attribute to an instance being loaded from the database". So your proposal
doesn't play nice with lazy loading of related instances, for example.
Another reason is the impossibility of running validations involving multiple
fields. If a validator depends on the value of two fields, one has to be set
before the other, and you can't validate at that point.
To sum up, I think this would be very hard to implement and not provide a
consistent user experience in the end.
On Sun, Mar 2, 2014 at 1:07 AM, Aymeric Augustin <aymeric....@polytechnique.org> wrote:
On 1 mars 2014, at 09:14, Martin Matusiak <nume...@gmail.com> wrote:A structural reason is that you can't tell "assigning an attribute to an
instance in order to save it to the database later" apart from "assigning an
attribute to an instance being loaded from the database". So your proposal
doesn't play nice with lazy loading of related instances, for example.
Another reason is the impossibility of running validations involving multiple
fields. If a validator depends on the value of two fields, one has to be set
before the other, and you can't validate at that point.For me, this is the bigger reason why validation at time of assignment isn't viable.In order to do multi-field validation at time of assignment, we'd need to introduce some sort of transactional behaviour. Just thinking about this makes my brain hurt.
On Saturday, March 1, 2014 4:29:43 PM UTC-7, Russell Keith-Magee wrote:On Sun, Mar 2, 2014 at 1:07 AM, Aymeric Augustin <aymeric....@polytechnique.org> wrote:
On 1 mars 2014, at 09:14, Martin Matusiak <nume...@gmail.com> wrote:A structural reason is that you can't tell "assigning an attribute to an
instance in order to save it to the database later" apart from "assigning an
attribute to an instance being loaded from the database". So your proposal
doesn't play nice with lazy loading of related instances, for example.
Another reason is the impossibility of running validations involving multiple
fields. If a validator depends on the value of two fields, one has to be set
before the other, and you can't validate at that point.For me, this is the bigger reason why validation at time of assignment isn't viable.In order to do multi-field validation at time of assignment, we'd need to introduce some sort of transactional behaviour. Just thinking about this makes my brain hurt.RussThis got me thinking about what the API for doing multi-field validation could look like. I came up with two ideas that may or may not hold water.The simplest solution I could think of would be a setter method on the model that took **kwargs and set them on the model, calling validation after they've all been set. Kind of a gross API, but it should work.
The maybe cleaner idea would be a context manager that deferred validation till the end of the context block.with instance.validation_block():x = 4y = 5