选择为整数的Django模型
我试图在Django中制作一个反馈应用程序,但我无法进行评估 在我的models.py中,我有5种选择,从非常差到优秀。但我希望它们可以作为数字使用,这样我就可以评估整体价值 阅读后,我将我的评分从VERYBAD=‘非常糟糕’改为VERYBAD=1,但不,我甚至无法保存我的值/表格选择为整数的Django模型,django,integer,django-views,choicefield,Django,Integer,Django Views,Choicefield,我试图在Django中制作一个反馈应用程序,但我无法进行评估 在我的models.py中,我有5种选择,从非常差到优秀。但我希望它们可以作为数字使用,这样我就可以评估整体价值 阅读后,我将我的评分从VERYBAD=‘非常糟糕’改为VERYBAD=1,但不,我甚至无法保存我的值/表格 Feedback(models.Model): event = models.ForeignKey(Event) # Choice VERYBAD = 'Very bad'
Feedback(models.Model):
event = models.ForeignKey(Event)
# Choice
VERYBAD = 'Very bad' #old
BAD = 2 #new
OKAY = 3
GOOD = 4
EXCELLENT = 5
RATING = (
(VERYBAD, 'Very bad'),
(BAD, 'Bad'),
(OKAY, 'Okay'),
(GOOD, 'Good'),
(EXCELLENT, 'Excellent'),
)
# The ratings
organisation = models.CharField(
max_length=9,
choices=RATING,
default=OKAY,
)
....
所以在我看来,我认为我可以做数学,但我做不到
def rating(request, event_id):
myevent = Event.objects.get(id=event_id)
feedback_items = Feedback.objects.filter(event=myevent)
num_of_items = len(feedback_items)
def evaluate(feedback_items):
# The overall rating
organisation = 0
for item in feedback_items:
organisation += item.organisation
organisation /= num_of_items
context = {'feedback_items':feedback_items,
'num_of_items': num_of_items,
'myevent': myevent,}
return render(request, 'feedback/rating.html', context)
`显然,您必须将
组织
设置为整数域
,否则您将无法希望执行整数运算。如果您已经在生产中使用了该字段,请使用一组迁移来添加新的IntegerField,从现有的CharField值填充它(使用dict将旧字符串值映射到新的int值),最后删除原始字段
此外,您可能希望了解如何正确使用Django的ORM功能,如获取相关模型实例和在DB级别执行聚合,即:
from django.db.models import Sum, Count, Avg
def rating(request, event_id):
myevent = Event.objects.get(id=event_id)
# this is how you get related models...
feedback_items = myevent.feedback_set.all()
# and this is how you use SQL aggregation functions:
values = feedback_items.aggregate(
sum=Sum('organisation'),
count=Count('pk'),
avg=Avg('organisation')
)
# Now 'values' should be a dict with 'sum', 'count' and 'avg'
# keys, and values['avg'] should be equal to
# float(values['sum']) / values['count']
# FWIW you probably don't need the 'sum' field at all
# I mentionned it so you can check whether 'avg' value
# is correct...
context = {
'feedback_items':feedback_items,
'num_of_items': values['count'],
'avg': values['avg'],
'myevent': myevent,
}
# etc
显然,您必须将
组织
设置为一个整数域
,否则您就不能希望执行整数运算。如果您已经在生产中使用了该字段,请使用一组迁移来添加新的IntegerField,从现有的CharField值填充它(使用dict将旧字符串值映射到新的int值),最后删除原始字段
此外,您可能希望了解如何正确使用Django的ORM功能,如获取相关模型实例和在DB级别执行聚合,即:
from django.db.models import Sum, Count, Avg
def rating(request, event_id):
myevent = Event.objects.get(id=event_id)
# this is how you get related models...
feedback_items = myevent.feedback_set.all()
# and this is how you use SQL aggregation functions:
values = feedback_items.aggregate(
sum=Sum('organisation'),
count=Count('pk'),
avg=Avg('organisation')
)
# Now 'values' should be a dict with 'sum', 'count' and 'avg'
# keys, and values['avg'] should be equal to
# float(values['sum']) / values['count']
# FWIW you probably don't need the 'sum' field at all
# I mentionned it so you can check whether 'avg' value
# is correct...
context = {
'feedback_items':feedback_items,
'num_of_items': values['count'],
'avg': values['avg'],
'myevent': myevent,
}
# etc
非常感谢你。在重读了making queries一章之后,我还设法进行了一个查询,该查询只显示了事件类别中我尚未注册的战斗机。竖起大拇指非常感谢你。在重读了making queries一章之后,我还设法进行了一个查询,该查询只显示了事件类别中我尚未注册的战斗机。竖起大拇指