问题描述
目前,我在我的models.py(剥离)中设置了以下模型结构:
Currently, I have the following model structure set up in my models.py (stripped down):
class Admin(models.Model):
admin_name = models.CharField(unique = True, blank = False, null = False, max_length = 128, verbose_name = u'admin full name')
def __unicode__(self):
return self.admin_name
class Meta:
ordering = ('id',)
verbose_name = u'Admin Info'
class Project(models.Model):
client = models.ForeignKey(Client, verbose_name = u'Client')
description = models.ForeignKey(Description, verbose_name = u'project description')
admins = models.ManyToManyField(Admin, verbose_name = u'Administrators', through = 'Admin_Payment')
class Admin_Payment(models.Model):
admin = models.ForeignKey(Admin, verbose_name = u'Administrator')
project = models.ForeignKey(Project, verbose_name = u'project')
agreed_payment = models.DecimalField(max_digits = 16, decimal_places = 2, blank = True, default = 0)
您可以看到,我已经使用Admin_Payment作为中间表,在我的项目和管理员之间建立了一个多重关系。
as you can see, I have setup a manytomany relation between my projects and admins using the Admin_Payment as the intermediate table.
我的问题是如何使agreement_dayment字段(在Admin_Payment类中)可用于我的项目类?
My question is how I can make the agreed_payment field (which is in the Admin_Payment class) available to my Project class?
推荐答案
Django创建一个attibute admin_payment_set
在作为相关管理器的Project对象中。访问其对象,例如所有()。例如
Django creates an attibute admin_payment_set
in the Project object that is a "related manager". Access its objects e.g. with "all()". E.g.
p = Project.objects.get(pk=1)
paysets = p.admin_payment_set.all()
paysets[0].agreed_payment
这篇关于django数据来自manytomanyfield中间表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!