0

我有一个看起来像这样的模型:

class Base(models.Model):
  pass

class Mixin1(Base):
  active = models.BooleanField(default=False)

class Mixin2(Base):
  name = models.CharField(max_length=200,blank=False,null=False)

class Organization(Mixin1, Mixin2, Base):
  pass

这工作正常:

organization = Organization(name='name')
organization.active = True
organization.save()

但这不会:

organization = Organization(name='name', active=True)
organization.save()

Django 在构造函数上出错:

TypeError: 'active' is an invalid keyword argument for this function

a 关于mixins,我有什么遗漏吗?

我所看到的更新: 使用新的 PostgreSQL 数据库和新的外壳。

Python 2.7.3 (default, Sep  5 2012, 20:48:07) 
[GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.60))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from core.models.organizations import Organization
>>> org1 = Organization(name='name')
>>> org1
<Organization: name>
>>> org1.active = True
>>> org1
<Organization: name>
>>> org1.active
True
>>> org2 = Organization(name='org2',active=True)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<myprojectpath>/venv/lib/python2.7/site-packages/django/db/models/base.py", line 367, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
TypeError: 'active' is an invalid keyword argument for this function
>>> 
4

2 回答 2

2

有效,我刚试过。

In [2]: model = Model(name='name')
In [3]: model.active = True
In [4]: model.save()

In [5]: model = Model(name='name2', active=True)
In [6]: model.save()

In [9]: Model.objects.all()
Out[9]: [<Model: Model object>, <Model: Model object>]

问题应该出在其他地方。

于 2012-09-08T22:09:14.470 回答
2

我有一个陈旧的 .pyc 文件,把事情搞砸了。我将文件从 organization.py 更改为 organizations.py。出于某种原因,organization.pyc 卡住并被另一个模型文件导入。令人沮丧。

于 2012-09-09T05:02:41.373 回答