class SampleModel(models.Model):
   date = models.DateField()
   value = models.IntegerField()


subquery = SampleModels.objects.filter(
    date__gte=models.OuterRef('date') - timedelta(days=2),
    date__lte=models.OuterRef('date') + timedelta(days=2),
    value__lte=30
)
queryset = SampleModel.objects.annotate(
    is_what_i_am_looking_for=models.Exists(subquery)
)

According to the documentation on OuterRef 
<https://docs.djangoproject.com/en/2.0/ref/models/expressions/#referencing-columns-from-the-outer-queryset>
:

Use OuterRef when a queryset in a Subquery needs to refer to a field from 
> the outer query. It acts like an F 
> <https://docs.djangoproject.com/en/2.0/ref/models/expressions/#django.db.models.F>
>  expression 
> except that the check to see if it refers to a valid field isn’t made until 
> the outer queryset is resolved.


Since it states that it acts "like" an F expression, I assumed that 
utilizing a timedelta would be permissible. However, I receive the 
following traceback:

AttributeError: 'ResolvedOuterRef' object has no attribute 'relabeled_clone'

 I was able to find someone else that is having this problem on 
StackOverflow 
<https://stackoverflow.com/questions/48230771/is-it-possible-to-do-arithmetic-operation-on-outerref-expression>.
 
I would like to confirm whether this is a bug or a lack of clarification in 
the documentation. F expressions allow operations such as timedelta on 
DateField.

Thanks for your help/advice in advance!

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1722c952-ebcd-4118-b8c2-d3497b22e640%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to