Tuesday, 7 May 2019

How do I resolve a Django query "'ExtractHour' object has no attribute 'split'" error?

I'm using Django and Python 3.7. I want to include a subquery in the criteria of a larger query.

from django.db.models.functions import ExtractHour
...
hour_filter = ExtractHour(ExpressionWrapper(
            F("article__created_on") + timedelta(0, avg_fp_time_in_seconds),
            output_field=models.DateTimeField()
        ),
)
query = StatByHour.objects.filter(hour_of_day=OuterRef(hour_filter))

...

The larger query that contains it is

qset = ArticleStat.objects.filter(                votes__gte=F("article__website__stats__total_score") / F(
                "article__website__stats__num_articles") *
                       Subquery(query.values('index'), outout_field=FloatField()) *
                       day_of_week_index)

However, when I run this, I get the error

'ExtractHour' object has no attribute 'split'

What does this mean and how I can adjust my filter so that this error goes away?

Edit: adding the model of the thing making the outer query ...

class ArticleStat(models.Model):
    objects = ArticleStatManager()
    article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='articlestats')
    ...
    votes = models.FloatField(default=0, null=False)



from How do I resolve a Django query "'ExtractHour' object has no attribute 'split'" error?

No comments:

Post a Comment