Django:TypeError:'branch__organisation'是此函数的无效关键字参数

时间:2014-07-15 11:55:56

标签: django python-2.7 django-models django-forms django-views

我有3个型号:

class Organisation(CommonInfo):
    name = models.CharField(max_length=50)
    gems = models.PositiveIntegerField(blank=True,default=0,null=True)
class Branch(models.Model):
    email = models.EmailField()
    address = models.TextField(default='', blank=True)
    organisation = models.ForeignKey(Organisation, related_name='branches')
    branch_details = models.OneToOneField('BranchDetails', related_name='branch')
class GemsBoughtHistory(CommonInfo):
    gems_bought = models.PositiveIntegerField(null=True,default='',blank=True)
    branch = models.ForeignKey(Branch, related_name='gems_in_branch')

我有组织的实例,我得到了:

查看:

organisation_id = self.kwargs['organisation_pk']
organisation = Organisation.objects.get(pk = organisation_id)
organisation_form = self.organisation_form(request.POST,instance=organisation)
if organisation_form.is_valid():
    org = organisation_form.save()
    gems =  org.gems
    gems_bought_history = GemsBoughtHistory.objects.create(gems_bought=gems,branch__organisation=organisation)
    //gems_bought_history = GemsBoughtHistory.objects.create(gems_bought=gems,branch__organisation=org)

现在,在创建GemsBoughtHistory行时,我收到此错误。 "无效的关键字参数"。 如何在GemsBoughtHistory模型中从分支遍历到组织? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

使用.create()时,您无法设置不在创建的模型上的值。因此,首先为所需组织创建(或从DB获取现有的一个),然后将该分支分配给.create()中的GemsBoughtHistory。