當前位置: 首頁>>代碼示例>>Python>>正文


Python Stock.quantity方法代碼示例

本文整理匯總了Python中models.Stock.quantity方法的典型用法代碼示例。如果您正苦於以下問題:Python Stock.quantity方法的具體用法?Python Stock.quantity怎麽用?Python Stock.quantity使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在models.Stock的用法示例。


在下文中一共展示了Stock.quantity方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: transaction

# 需要導入模塊: from models import Stock [as 別名]
# 或者: from models.Stock import quantity [as 別名]
def transaction(request):

    if request.user.is_authenticated():
        # if this is a POST request we need to process the form data
        if request.method == 'POST':
            # create a form instance and populate it with data from the request:
            form = TransactionForm(request.POST)
            EntryFormSet = formset_factory(EntryForm, extra=10)
            formset = EntryFormSet(request.POST)

            # check whether it's valid:
            if form.is_valid() and formset.is_valid():
                # process the data in form.cleaned_data as required

                in_type = form.cleaned_data['type']
                in_party = form.cleaned_data['party']
                in_description = form.cleaned_data['description']

                # create the transaction
                # todo be careful because there is a DB hit with create, consider using save
                transaction = Transaction.objects.create(transaction_type=in_type,
                                                         transaction_actor=request.user.username,
                                                         transaction_party=in_party,
                                                         transaction_description=in_description)

                # now save the data for each entry in the formset
                new_entries = []

                for entry_form in formset:
                    in_transaction_gkey = transaction
                    in_product = entry_form.cleaned_data.get('product')
                    in_color = entry_form.cleaned_data.get('color')
                    in_quantity = entry_form.cleaned_data.get('quantity')

                    if in_transaction_gkey and in_product and in_color and in_quantity:

                        # Adjust the stock
                        try:
                            product = Product.objects.get(product_name=in_product, product_color=in_color)
                        except Product.DoesNotExist:
                            product = Product(product_name=in_product, product_color=in_color)
                            product.save()
                        try:
                            stk = Stock.objects.get(product_gkey=product)
                        except Stock.DoesNotExist:
                            stk = Stock(product_gkey=product)
                            stk.save()

                        if transaction.transaction_type == 'In':
                            stk.quantity = stk.quantity + in_quantity
                        else:
                            stk.quantity = stk.quantity - in_quantity
                        stk.save()

                        new_entries.append(Entry(transaction_gkey=in_transaction_gkey,
                                                 product_gkey=product,
                                                 quantity=in_quantity))
                        #todo: add try catch
                        #todo: add verification no 2 same product
                        Entry.objects.bulk_create(new_entries)

                # redirect to a new URL:
                return render(request,'mafia/index.html')

        # if a GET (or any other method) we'll create a blank form
        else:
            form = TransactionForm()
            EntryFormSet = formset_factory(EntryForm,extra=10)
            formset = EntryFormSet()
        return render(request, 'mafia/transaction.html',  {'form': form, 'formset': formset})
    else:
        return render(request, 'mafia/request_login.html')
開發者ID:guillaume-miara,項目名稱:mista,代碼行數:74,代碼來源:views.py


注:本文中的models.Stock.quantity方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。