take a look at this https://docs.djangoproject.com/en/3.0/topics/signals/


El lun., 27 ene. 2020 a las 18:59, Stephen J. Butler (<
[email protected]>) 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?
>
>
> https://docs.djangoproject.com/en/3.0/ref/models/expressions/#using-f-with-annotations
>
> Item.objects.annotate(is_on_sale=(F('price') < F('full_price')))
>
>
>
> On Mon, Jan 27, 2020 at 11:47 AM Peter Law <[email protected]> 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 [email protected].
>> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAD4ANxWtpqnDr_8WrQiskH7wXxBPyJ3sTWa%2BAsK1592SXWTfpw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAD4ANxWtpqnDr_8WrQiskH7wXxBPyJ3sTWa%2BAsK1592SXWTfpw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAO_yRT1F09KXXAJ9e_tdGzxjpy9TBf5B6%3D-Yyzp-Swepybay5w%40mail.gmail.com.

Reply via email to