3 ms·
Mostly because it is dangerous (or was). For example, an integer that can only be 1, 2, or 3. In the admin (way back in the day, at least), someone could go int
by withinboredom 3y ago
Mostly because it is dangerous (or was). For example, an integer that can only be 1, 2, or 3. In the admin (way back in the day, at least), someone could go into the admin and set it to 4. Which would then break everything in subtle ways.
Also, most applications have constraints like "X can only be 2 if and only if Y is equal to Z" ... there was no way to specify that constraint to the admin interface, so someone could set X to be 2, and it would break programmer assumptions who wrote code expecting Y to be equal to Z.
- rburhum 3y agoif your model is a choicefield, then it only shows you those values and you can’t do what you are describing. Additionally, you have validation that can happen at the class level, group of fields, or field.
- petepete 3y agoI don't know Django but doesn't model validation apply to the admin screens?
- loloquwowndueo 3y agoIt does but in general there’s less insistence (as compared to Rails, for example) to have “smart models, dumb controllers” - therefore in Django a lot of business logic can end up in the controller and thus doesn’t apply in the admin. This is a convention though - one could just as easily do it the other way and embed all the business rules in the models.
- withinboredom 3y agoIt used to not be IIRC. This was over a decade ago, so my information is likely outdated, but it might explain why other languages chose not to implement it. Further, a model may be valid, but make no sense to an application. For example, a car may have 6 wheels and still be a car ... but might be out-of-bounds for a simulation expecting 4 wheels.
- evantbyrne 3y agoMust have been well over a decade because Django models have had deep integration with the form validation API since I started my career.
- stuartjohnson12 3y agoI can confirm you can do this validation now, but it's not super-super easy.
- rburhum 3y agoyes it does if implemented correctly with that intention in mind
- timetraveller26 3y agoI think you can can still have those checks as pre_saves in your models
- simonw 3y agoThis is very out-of-date. Easily adding validation has been a key benefit of the Django admin for more than a decade at this point. (You still have to do the work, but if people are breaking things through the admin that's your fault.)