Django从www.example.com中的外键获取值views.py

deyfvvtc  于 2023-04-13  发布在  Go
关注(0)|答案(1)|浏览(131)

我是一个初学者在dango。我试图检索价格从服务,使所有的价格总和。我寻求帮助。

logopedija/models.py

class Pricelist(models.Model):
name=models.CharField('Name of service', max_length=60)
price=models.DecimalField(default=0, decimal_places=2, max_digits=20)

def __str__(self):
return "%s %s" % (self.name, self.price)

class Service(models.Model):
id = models.AutoField(primary_key=True)
user=models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, limit_choices_to={'active': True},)
service=models.ForeignKey(Pricelist, on_delete=models.CASCADE, null=True, blank=True)
day=models.DateField(default=date)
time_service = models.CharField(max_length=10, choices=TIME-CHOICE, default="16:00")
is_paid = models.BooleanField(default=False)

def __str__(self):
return f"{self.user.name} | dan:{self.day} | time:{self.time_service} | service:{self.service.price}"

logopedija/views.py

def show_service(request, service_id):
service = Service.objects.get(pk=service_id)
return render(request, 'users/show_service.html', {'service':service})

logopedija/show_service.html

{% extends 'pacijenti/base.html' %}

    {% block content %}

\<div class="card"\>
\<div class="card-header"\>
Detalji termina
\</div\>
\<div class="card-body"\>
\<h5 class="card-title"\>{{ service }}\</h5\>  
\<p class="card-text"\>
{{ service.id }}\<br/\>
{{ service.user }}\<br/\>
{{ service.service }`your text`}\<br/\>
{{ service.day }}\<br/\>
{{ service.time_service }}\<br/\>
\</p\>
\</div\>
\</div\>
{% endblock %}
aurhwmvo

aurhwmvo1#

要汇总所有服务的价格,您可以使用Django的聚合函数。这里有一个例子:

from django.db.models import Sum
from logopedija.models import Service

total_price = Service.objects.filter(is_paid=True).aggregate(sum=Sum('service__price'))['sum']

这段代码将对is_paid=True中所有服务的价格求和。Sum函数用于执行聚合,并应用于service__price字段,该字段是Pricelist模型的外键,用于保存价格。
您可以在视图函数中使用此代码来计算总价并将其传递给模板,如下所示:

from django.shortcuts import render
from django.db.models import Sum
from logopedija.models import Service

def show_services(request):
    services = Service.objects.filter(is_paid=True)
    total_price = services.aggregate(sum=Sum('service__price'))['sum']
    return render(request, 'users/show_services.html', {'services': services, 'total_price': total_price})

然后,在模板中,您可以显示总价沿着服务:

{% extends 'pacijenti/base.html' %}

{% block content %}

<div class="card">
    <div class="card-header">
        Detalji termina
    </div>
    <div class="card-body">
        <h5 class="card-title">Ukupna cena: {{ total_price }}</h5>
        {% for service in services %}
        <p class="card-text">
            {{ service.id }}<br>
            {{ service.user }}<br>
            {{ service.service.name }} - {{ service.service.price }}<br>
            {{ service.day }}<br>
            {{ service.time_service }}<br>
        </p>
        {% endfor %}
    </div>
</div>

{% endblock %}

请注意,我使用了Pricelist模型的name字段来显示服务的名称,而不是__str__方法。此外,我还在服务的名称和价格之间添加了一个破折号。您可以根据需要进行调整。您还可以修改模型以

class Pricelist(models.Model):
    service_name=models.ForeignKey(Pricelist, on_delete=models.CASCADE, null=True, blank=True)
    price=models.DecimalField(default=0, decimal_places=2, max_digits=20)

              ....

class Service(models.Model):
    id = models.AutoField(primary_key=True)
    name=models.CharField('Name of service', max_length=60)

相关问题