Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

python - See how many orders he has on his profile page. Django

When I enter my user profile page, I want it to see the total number of orders until today. i tried aggregate and annonate but it's not work. I hope so i use filter method but i don't know how to do it.

Orders count = adet in model

I added ""if siparis.bayi_id == user.id"" so that the user entering can take action on his orders.
Temp Html

                                {% for siparis in siparis %}
                                    {% if siparis.bayi_id == user.id %}

                          
                                            <strong>{{ a }}</strong><br><small>Sipari?ler Toplam?</small>


                            {% endif %}
                            {% endfor %}
                            

Model Siparis means order

class Siparis(models.Model):
bayi = models.ForeignKey('auth.User', verbose_name='bayi', on_delete=models.CASCADE, related_name='bayi',limit_choices_to={'groups__name': "BayiGrubu"})
urun = models.ForeignKey(Urun, on_delete=models.CASCADE)
adet = models.IntegerField()
tarih = models.DateTimeField()
status = models.BooleanField()

@property
def miktar(self):
    return (self.adet * self.urun.fiyat)

@property
def fiyat(self):
    return self.urun.fiyat

class Meta:
    verbose_name = 'Bayi Sipari?'
    verbose_name_plural = 'Bayi Sipari?leri'

views

def bayi_bayidetay(request):

siparis = Siparis.objects.all()
urunler = Urun.objects.all()
bayiler = bayi_bilgi.objects.all()

a = Siparis.objects.aggregate(Sum("adet"))
return render(request,'bayi/bayi_detay.html',{'bayiler':bayiler,'siparis':siparis,'urunler':urunler, 'a': a})

Thank you

question from:https://stackoverflow.com/questions/65645501/see-how-many-orders-he-has-on-his-profile-page-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try add filter after a, like this:

a = Siparis.objects.filter(bayi=request.user).aggregate(Sum("adet"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...