【问题标题】:Django: TypeError: 'branch__organisation' is an invalid keyword argument for this functionDjango:TypeError:'branch__organisation' 是此函数的无效关键字参数
【发布时间】:2014-07-15 11:55:56
【问题描述】:

我有 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 模型中如何从分支遍历到组织? 提前谢谢你。

【问题讨论】:

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


    【解决方案1】:

    使用 .create() 时,您不能设置不在创建模型上的值。因此,首先为想要的组织创建(或从数据库中获取现有的),然后将该分支分配给 .create() 中的 GemsBoughtHistory。

    【讨论】:

    • 感谢您的澄清。我获取了分支对象,然后将其保存在 GemsBoughtHistory 模型中。
    猜你喜欢
    • 1970-01-01
    • 2013-04-03
    • 2017-05-04
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2012-07-07
    • 2013-07-13
    相关资源
    最近更新 更多