Monday, January 27, 2020

Re: How to express `foo = (bar < quox)` as a constraint

sorry i diin't read to Stephen his trik is even better hahahaha but with sigñal yo ca do other thing

El lun., 27 ene. 2020 a las 19:02, DANIEL URBANO DE LA RUA (<dannybombastic@gmail.com>) escribió:
you can do few thing at the same time in a model before save or whenever you want

El lun., 27 ene. 2020 a las 19:01, DANIEL URBANO DE LA RUA (<dannybombastic@gmail.com>) escribió:
it is goin to be better and less cost for the db 

El lun., 27 ene. 2020 a las 19:01, DANIEL URBANO DE LA RUA (<dannybombastic@gmail.com>) escribió:

El lun., 27 ene. 2020 a las 18:59, Stephen J. Butler (<stephen.butler@gmail.com>) escribió:
Frankly, if is_on_sale has such a tight constraint I wouldn't have it as its own column. Why not just make it an annotated field with an F expression?


Item.objects.annotate(is_on_sale=(F('price') < F('full_price')))



On Mon, Jan 27, 2020 at 11:47 AM Peter Law <peter@thread.com> wrote:
Hi,

Thanks for adding support for check constraints in Django 2.2, it's
great to be able to move constraints into the model definitions.

I've been trying to work out how to express a constraint which
validates that the value of one field expresses a relation between two
other fields, but can't find a nice way to do so.

I've read through the docs and also found
https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion,
but haven't found a concise spelling.

I've got a model like:

```
class Item(Model):
    price = DecimalField()
    full_price = DecimalField()
    is_on_sale = BooleanField()
```

I'd like to be able to express neatly that the `is_on_sale` boolean be
true only when `price < full_price`.
In Postgres I can express this as:

```
ALTER TABLE item
ADD CONSTRAINT is_on_sale_check
CHECK (is_on_sale = (price < full_price))
```

However in Django I can't find a way to express this directly.

I did find a long spelling which essentially checks the True case and
the False case explicitly and then ORs them together, however it's
several lines of `models.Q` combinations and not at all clear about
what the code is trying to achieve.

Is there a concise way to do this sort of constraint? If not, would it
be possible for Django to add support for it?

Thanks,
Peter

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEMtty7rsf2qk_0qXnzqZdA89e6VRCqF%2B4nQSHgxVCE4XfKAnw%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD4ANxWtpqnDr_8WrQiskH7wXxBPyJ3sTWa%2BAsK1592SXWTfpw%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAO_yRT1vDfqo2vRtmgUcwzc9FndWAgd3qZkxvxYptSFvcjDNKg%40mail.gmail.com.

No comments:

Post a Comment