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

2020-01-28 Thread Peter Law
Hi Simon, Thanks! Using an ExpressionWrapper does work for this :) I agree it might be nice to have a bit less boilerplate here. What I'd first tried was actually: ``` CheckConstraint(check=Q( is_on_sale=F('price') < F('full_price'), )) ``` It would be great if that could be made to work -

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

2020-01-27 Thread Integr@te System
Hi All, Let try sth like sample: a = True if b <= c else False On Tue, Jan 28, 2020, 03:24 Simon Charette wrote: > I see, I think you'll need to wrap the inner Q in an ExpressionWrapper[0] > > That would be > > CheckConstraint(check=Q(is_on_sale=ExpressionWrapper(Q(price__lt=F('full_price')),

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

2020-01-27 Thread Simon Charette
I see, I think you'll need to wrap the inner Q in an ExpressionWrapper[0] That would be CheckConstraint(check=Q(is_on_sale=ExpressionWrapper(Q(price__lt=F('full_price')), output_field=models.BooleanField( That involves a lot of boilerplate though and it ought to work without all of it so

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

2020-01-27 Thread Peter Law
Hi Simon, Thanks for your response. I did try that, however unfortunately I get an error when running the migration: django.core.exceptions.ValidationError: ["'(AND: )' value must be either True or False."] I'm using Django 2.2 LTS though testing this in 3.0 unfortunately errors in the same

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

2020-01-27 Thread Peter Law
Hi Stephen, Thanks for your response. I agree that using an annotation might be an alternative way to solve this, however for various reasons that's not suitable for my current use case. I was also hoping for a general solution which would work for cases where the expression is more complicate

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

2020-01-27 Thread Simon Charette
Did you try class Item(Model): price = DecimalField() full_price = DecimalField() is_on_sale = BooleanField() class Meta: constraints = [ CheckConstraint(check=Q(is_on_sale=Q(price__lt=F('full_price' ] I haven't tried it myself but I would expect

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

2020-01-27 Thread DANIEL URBANO DE LA RUA
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 (< dannybombas...@gmail.com>) escribió: > you can do few thing at the same time in a model before save or whenever > you want > > El lun.

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

2020-01-27 Thread DANIEL URBANO DE LA RUA
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 (< dannybombas...@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 (<

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

2020-01-27 Thread DANIEL URBANO DE LA RUA
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 (< stephen.but...@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 fi

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

2020-01-27 Thread DANIEL URBANO DE LA RUA
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 (< dannybombas...@gmail.com>) escribió: > 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 (< > stephen.b

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

2020-01-27 Thread Stephen J. Butler
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_pric

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

2020-01-27 Thread Peter Law
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 fi