简体   繁体   English

Django-Q 和 request.POST 数据

[英]Django-Q and request.POST data

I have a budget app I am working on and using Django and Django-Q.我有一个预算应用程序,我正在使用 Django 和 Django-Q。 I want to create a schedule to post automatically an expense based on a Django-Q schedule I create.我想创建一个时间表,以根据我创建的 Django-Q 时间表自动发布费用。 The issue I am having is understanding the logic of using three positional arguments in my Djano-Q Schedule.我遇到的问题是理解在我的 Djano-Q 计划中使用三个位置 arguments 的逻辑。

The three positional arguments are request, budget_id and category_id.三个位置 arguments 是 request、budget_id 和 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.我想将数据发布到我的 add_automatic_expense 视图,这将根据表单数据创建一个初始费用,然后创建一个 Django-Q 计划来触发费用创建,以便每月使用相同的数据运行。

Can anybody help shed some light on this process?任何人都可以帮助阐明这个过程吗? Thanks.谢谢。

views.py视图.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:当我使用表单数据 POST 到那个 function 时,它增加了初始费用,但是当我尝试运行我的 QCluster 之后,我收到以下错误:

14:34:39 [Q] ERROR malformed node or string: <_ast.Name object at 0x7fa06bf2a0d0> 14:34:39 [Q] 错误格式错误的节点或字符串:<_ast.Name object at 0x7fa06bf2a0d0>

I figured it out.我想到了。 In my add_automatic_expense function i changed the Schedule create to look like this:在我的 add_automatic_expense function 中,我将 Schedule create 更改为如下所示:

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.然后我创建了另一个名为 add_auto_expense 的 function 视图,并将 **kwargs 作为其位置值。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM