通过Django查询多个多个字段 [英] Query for a ManytoMany Field with Through in Django
问题描述
class Classification(models.Model):
name = models.CharField(choices = class_choices)
...
类Activity(models.Model):
name = models.CharField(max_length = 300)
fee = models.ManyToManyField(Classification,through ='Fee')
...
class费用(models.Model):
activity = models.ForeignKey(Activity )
class = models.ForeignKey(Classification)
early_fee = models.IntegerField(decimal_places = 2,max_digits = 10)
regular_fee = models.IntegerField(decimal_places = 2,max_digits = 10)
想法是每个活动和分类对都会有一套费用。分类就像学生,工作人员等。
我知道这部分是正确的。
然后在我的应用程序中,我查询一组活动:
activities = Activity.objects.filter(...)
哪个返回活动列表。我需要在我的模板中显示活动列表及其费用。这样的事情:
活动名称
学生提前价格 - $ 4
学生常规价格 - $ 5
工作人员提前价格 - $ 6
工作人员常规价格 - $ 8
但是我不知道一个简单的方法来获取这个信息,而不需要每个活动/类对的费用对象的特定获取查询。
我希望这可以工作:
activity.fee.all ()
但是只返回分类对象。有没有办法通过我已经查询的活动来获取配对的费用对象数据?
或者我这样做完全错了?
考虑到michuk提示将费用重新命名为分类:
费用的默认名称活动模型上的对象将被收费。所以为了获得你的价格,请执行以下操作:
for a in Activity.objects.all():
a.fee_set.all()#gets你所有的活动费
有一件事,因为你可以看到你最终会在每个活动对象上执行1个SELECT以获取费用,有一些可以帮助的应用程序,例如 django-batch-select 在这种情况下只执行2次查询。
I have a models in Django that are something like this:
class Classification(models.Model):
name = models.CharField(choices=class_choices)
...
class Activity(models.Model):
name = models.CharField(max_length=300)
fee = models.ManyToManyField(Classification, through='Fee')
...
class Fee(models.Model):
activity = models.ForeignKey(Activity)
class = models.ForeignKey(Classification)
early_fee = models.IntegerField(decimal_places=2, max_digits=10)
regular_fee = models.IntegerField(decimal_places=2, max_digits=10)
The idea being that there will be a set of fees associated with each Activity and Classification pair. Classification is like Student, Staff, etc.
I know that part works right.
Then in my application, I query for a set of Activities with:
activities = Activity.objects.filter(...)
Which returns a list of activities. I need to display in my template that list of Activities with their Fees. Something like this:
Activity Name
Student Early Price - $4
Student Regular Price - $5
Staff Early Price - $6
Staff Regular Price - $8
But I don't know of an easy way to get this info without a specific get query of the Fees object for each activity/class pair.
I hoped this would work:
activity.fee.all()
But that just returns the Classification Object. Is there a way to get the Fee Object Data for the Pair via the Activities I already queried?
Or am I doing this completely wrong?
Considering michuk's tip to rename "fee" to "classification":
Default name for Fee objects on Activity model will be fee_set. So in order to get your prices, do this:
for a in Activity.objects.all():
a.fee_set.all() #gets you all fees for activity
There's one thing though, as you can see you'll end up doing 1 SELECT on each activity object for fees, there are some apps that can help with that, for example, django-batch-select does only 2 queries in this case.
这篇关于通过Django查询多个多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!