mcab / hiber

API for Bat House SDP
GNU General Public License v3.0
0 stars 1 forks source link

Fix size and pups attributes for Bats #22

Open mcab opened 5 years ago

mcab commented 5 years ago

When using *RangeField in specific PostgreSQL fields, the bounds are always defined as [) (inclusive first number, exclusive last number). This can be changed in the database, but Django does not support changing those bounds.

So, when users enter in data in the administration panel, erroneous behavior can occur.

Say for example, a bat only has one pup per year. That would be entered in as 1 - 1 in the Wagtail interface. Stored in the database, that's [1, 1), which is an invalid range, and defaults to null for the value. This shouldn't be the case.

One solution would be to break it up into two separate fields (min/max_(size|pups)), and create a serializer that ties the two fields for the API, like

"pups": {
    "min": 1
    "max": 1
}
mcab commented 5 years ago

This isn't as high priority, because the Bat Admin panel isn't necessary for demonstration purposes.

mcab commented 5 years ago

This should be done by replacing size and pups with something utilizing DecimalField.

min_size = models.DecimalField(max_digits=3, decimal_places=1)
max_size = models.DecimalField(max_digits=3, decimal_places=1)

The way to provide a custom validator for this (min_size <= max_size) would be to implement a clean() method on it. Probably not worth the trouble, unless tests are written for model validation.