0

I'm trying this: but its throwing an TypeError: auto_sms() missing 1 required positional argument: 'request' error.

Now I'm thinking of getting the function from views.py instead and calling it on tasks.py if requests is not working on tasks.py, how can I do it? Thanks!

@shared_task
def auto_sms(request):
    responses = Rainfall.objects.filter(
        level='Torrential' or 'Intense',
        timestamp__gt=now() - timedelta(days=1),
    )

    count = responses.count()
    if not (count % 10) and count > 0:
        send_sms(request)

    return
2
  • 1
    how exactly are you calling your task Commented Jan 10, 2021 at 21:26
  • btw thanks for commenting but I just forgot to import request from django.http that's why it's not working before. Commented Jan 10, 2021 at 21:28

1 Answer 1

1

Passing the entire request is probably not a good idea since it can include Django model objects such as a user object. Now the problem that you will face is that if there is an object that is not serializable, then you'll get an error while calling the function. So instead of passing the whole request, just send the data that you actually need.

For example, I'm guessing you need the user here to send an SMS to. So instead of passing the whole request with the user object included, then just send the user_id and then get the user there. basically, you have to make sure that the data you're passing is serializable.

It's generally a good idea to pass ids of the Django models since the data might change while your function is being processed and you might get the old data if you pass the whole data.

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

Comments

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.