0

I'm converting a FlaskApp to Django. In my Flask App I was doing straight DB queries w/o an ORM like MySQLAlchemy.

I'm having trouble converting my count queries for use in Django. Seems simple enought, but I'm missing something.

OLD FUNCTION W/ QUERY

def countClients(db, begin, end, type):
    c = db.cursor()
    query = "SELECT COUNT(*) FROM newclients WHERE method LIKE %s AND (enddate BETWEEN %s AND %s)"
    c.execute(query, (type, begin, end))
    output = int(c.fetchone()[0])
    return output

NEW QUERY

for year in range(2015, today.year):
    for month in range(1, 13):
        startdate = datetime.date(year, month, 1)
        enddate = last_day_of_month(startdate)
        try:
            Count14.append(
                newclients.objects.filter(method = 'Test Drive',
                                          enddate__range=(startdate, enddate)
                                          )
            ).count()

WHAT I'VE TRIED

  • Query without Try:Except

w/o the try except I get a traceback

'NoneType' object has no attribute 'count'
  • Adding a int() around the query

But the int function can't accept a queryset.

NOTE I have used portions of these queries successfully, but these what I'm trying here is more complex than the others.

i.e. This one works, but is simpler.

clients = newclients.objects.filter(method=method_query).filter(enddate__range=(begindate, enddate))
1
  • Should the .count() be on the newclients.objects.filter(...) result rather than the Count14.append() result? Commented Aug 17, 2016 at 16:04

1 Answer 1

1

Your .count() is in the wrong place. It needs to be after the second close parenthesis, not the third.

As it is, you are calling it on the result of append, which is always None.

Sign up to request clarification or add additional context in comments.

1 Comment

ARGH! Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.