質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

1701閲覧

djangoで数値を計算したい

uyuki

総合スコア1

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2020/12/07 21:14

前提・実現したいこと

イメージ説明
実現したいことは二つあります。
0. 上記の画像のお会計を足した数字を売上合計のところに表示したいです。
2. 売上合計の箇所に一度足したら、二度目は足さなくても良いようにしたいです。

具体的にどのようにしたいかを申し上げますと、一枚目はお会計が12100なので、売上合計も12100になるのは良いのですが、二枚目の売上合計の表示のところは12100 + 6613 を足した18713にしたいです。しかし、6313になってしまいます。

お会計のタブを再度クリックすると、売上合計にお会計が再度足されます。

コードは下記のように書きました。

view.py def billfunc(request, pk): object = SalesModel.objects.get(pk=pk) if object.girlsdrink_confirmation == 'yes' and object.tax_confirmation == 'yes': object.bill = (object.tablecharge * object.custermer + object.girlsdrink_count * 1000 + object.staff_reservation_fee + object.champagne_fee + object.singlecharge) * 1.1 object.sales_total += object.bill object.save() return redirect('list') elif object.girlsdrink_confirmation == 'yes' and object.tax_confirmation == 'no': object.bill = (object.tablecharge * object.custermer + object.girlsdrink_count * 1000 + object.staff_reservation_fee + object.champagne_fee + object.singlecharge) object.sales_total += object.bill object.save() return redirect('list') elif object.girlsdrink_confirmation == 'no' and object.tax_confirmation == 'yes': object.bill = (object.tablecharge * object.custermer + object.girlsdrink_count * 0 + object.staff_reservation_fee + object.champagne_fee + object.singlecharge) * 1.1 object.sales_total += object.bill object.save() return redirect('list') else: object.girlsdrink_confirmation == 'no' and object.tax_confirmation == 'no' object.bill = (object.tablecharge * object.custermer + object.girlsdrink_count * 0 + object.staff_reservation_fee + object.champagne_fee + object.singlecharge) object.sales_total += object.bill object.save() return redirect('list')
list.html {% extends 'base.html' %} {% block content %} <div class="container"> {% for item in object_list %} <div class="alert alert-success" role="alert"> <p>テーブル番号 : {{ item.tablenumber }}</p> <p>お会計 : <a href="{% url 'bill' item.pk %}">{{ item.bill }}</a></p> <p>消費税 : <a href="{% url 'tax' item.pk %}">{{ item.tax_total }}</a></p> <p>投稿時間 : {{ item.date }}</p> <p>売上合計 : {{item.sales_total}}</p> <a href="{% url 'detail' item.pk %}" class="btn btn-primary" role="button" aria-pressed="true">詳細画面へ</a> <a href="{% url 'delete' item.pk %}" class="btn btn-success" role="button" aria-pressed="true">削除画面へ</a> <a href="{% url 'update' item.pk %}" class="btn btn-info" role="button" aria-pressed="true">編集画面へ</a> </div> {% endfor %} </div> {% endblock content %}
model.py class SalesModel(models.Model): tablenumber = models.CharField('テーブル番号', max_length=100) girlsdrink_confirmation = models.CharField('ドリンク別か込みか', max_length=50, choices=CHOICES) tax_confirmation = models.CharField('TAXありかなしか', max_length=50, choices=CHOICES2) tablecharge = models.IntegerField('セット料金') custermer = models.IntegerField('お客さんの人数') girlsdrink_count = models.IntegerField('ドリンクの杯数') staff_reservation_fee = models.IntegerField('指名料', null=True, blank=True) champagne_fee = models.IntegerField('シャンパン料金', null=True, blank=True) tax_total = models.IntegerField(null=True, blank=True) bill = models.IntegerField('お会計', null=True, blank=True) sales = models.IntegerField(null=True, blank=True) date = models.DateTimeField(default=timezone.now) singlecharge = models.IntegerField('シングルチャージ', null=True, blank=True) sales_total = models.IntegerField('売上', null=True, blank=True, default=0)
試したこと

試したことは、Sum関数を下記ようにインポートし、aggregateを使って合計値を出そうとしたのですが,この先どのようにしたら良いかわからず断念してしまいました。 プログラミングも始めたばかりで、djangoも初心者なので、皆様の力添えをよろしくお願いします。

from django.db.models import Sum obj = Salesmodel.objects.aggregate(total_price=Sum("bill"))

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

下記のように累加はWindowを使用すればできます。

python

1# models.py 2from autorepr import autorepr 3 4from django.db import models 5 6 7class Table(models.Model): 8 number = models.IntegerField('テーブル番号') 9 subtotal = models.IntegerField('小計') 10 tax = models.IntegerField('消費税') 11 total = models.IntegerField('合計') 12 13 __repr__ = autorepr(['number', 'subtotal', 'tax', 'total', ])

yaml

1--- 2# fixtures/sales.yaml 3- model: sales.table 4 pk: 1 5 fields: 6 number: 1 7 subtotal: 10000 8 tax: 1000 9 total: 11000 10- model: sales.table 11 pk: 2 12 fields: 13 number: 2 14 subtotal: 15000 15 tax: 1500 16 total: 16500 17- model: sales.table 18 pk: 3 19 fields: 20 number: 3 21 subtotal: 5000 22 tax: 500 23 total: 5500 24- model: sales.table 25 pk: 4 26 fields: 27 number: 4 28 subtotal: 1000 29 tax: 100 30 total: 1100

python

1from django.db import models 2from sales.models import Table 3tables = Table.objects.annotate( 4 accumulation=models.Window( 5 expression=models.Sum('total'), order_by=('number',) 6 ) 7) 8for table in tables: 9 print(repr(table), 'accumulation:', table.accumulation) 10 11<sales.models.Table number=1 subtotal=10000 tax=1000 total=11000 at 0x7fcc0f1ed790> accumulation: 11000 12<sales.models.Table number=2 subtotal=15000 tax=1500 total=16500 at 0x7fcc0f1ed310> accumulation: 27500 13<sales.models.Table number=3 subtotal=5000 tax=500 total=5500 at 0x7fcc0f1eda30> accumulation: 33000 14<sales.models.Table number=4 subtotal=1000 tax=100 total=1100 at 0x7fcc0f1ed580> accumulation: 34100

投稿2020/12/10 02:46

hasami

総合スコア1277

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

uyuki

2020/12/10 05:05

回答ありがとうございます。 yamlを初めて目にするので、yamlについて全然理解していないのですが、 yamlファイルのpkやnumber, subtotalなどの数値はユーザによって入力された数値を手動で入力する物なのでしょうか?
hasami

2020/12/10 23:54 編集

yamlファイルはフィクスチャーと呼ばれる、モデルに初期データを登録するときに使用するファイルです。 他にJSON、XML形式で作成できます。 manage.py loaddataコマンドでファイルを指定すると、初期データが登録されます。 manage.py dumpdataコマンドを使用すると、データベースに登録されているデータがフィクスチャーとして、標準出力に出力されます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問