Django-Q and request.POST data

399 views Asked by At

I have a budget app I am working on and using Django and Django-Q. I want to create a schedule to post automatically an expense based on a Django-Q schedule I create. The issue I am having is understanding the logic of using three positional arguments in my Djano-Q Schedule.

The three positional arguments are request, budget_id and category_id.

I want to post data to my add_automatic_expense view, which will create an initial expense based on the form data, and then create a Django-Q schedule to trigger an expense creation to run every month with that same data going forward.

Can anybody help shed some light on this process? Thanks.

views.py

def add_automatic_expense(request, budget_id, category_id):
get_budget = Budget.objects.get(id=budget_id)
get_category = Category.objects.get(id=category_id)
form = ExpenseEventForm()

if request.method == "POST":

    Schedule.objects.create(func='budgeteer.views.add_automatic_expense', args='request, budget_id, category_id',repeats=-1, schedule_type="D")

    form = ExpenseEventForm(request.POST)


    expense = form.save(commit=False)
    expense.amount = request.POST.get('amount')
    expense.description = request.POST.get('description')
    expense.transaction_date = request.POST.get('transaction_date')
    expense.categories_id = category_id
    expense.automatic_payment = True
    expense.save()
    expense_id = Expense.objects.last()
    get_budget.expense_set.add(expense_id)
    get_category.category_expense.add(expense_id)

    return redirect('view_expenses', budget_id=budget_id, category_id=category_id)
else:
    form = ExpenseEventForm()
return render(request, 'budgeteer/add_automatic_expense.html', {'form':form})

When I POST to that function with form data it adds the initial expense, however when I attempt to run my QCluster after that I get the following error:

14:34:39 [Q] ERROR malformed node or string: <_ast.Name object at 0x7fa06bf2a0d0>

1

There are 1 answers

0
Tony On BEST ANSWER

I figured it out. In my add_automatic_expense function i changed the Schedule create to look like this:

Schedule.objects.create(func='budgeteer.views.add_auto_expense', name=None, hook=None, schedule_type='O', minutes=None, repeats=-1,
    kwargs={"amount":request.POST.get('amount'), "description": request.POST.get('description'), "transaction_date":request.POST.get('transaction_date'),
    'expense_frequency_choice_field':request.POST.get('expense_frequency_choice_field'),'automatic_payment':request.POST.get('automatic_payment'), 'categories_id':request.POST.get('categories')})

I then created another function view called add_auto_expense with **kwargs as its positional value. This gave me access to all of the data found in my schedule.

def add_auto_expense(**kwargs):
    new_expense = Expense.objects.create(amount=kwargs['amount'], description=kwargs['description'],
    transaction_date=kwargs['transaction_date'], categories_id=106)