我应该说我是Django noob!我创建了一个DeveloperManager()管理器类和一个Developer()模型类。 我正在尝试使用
在数据库中创建开发人员记录d = Developer.objects.create_default_developer("a test name", "username", "1234", "email@example.com")
但它失败了
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/user/Desktop/Projects/project/engine/models.py", line 9, in create_default_developer
def create_default_developer(self, developer_name, username, password, email):
File "/Users/user/.virtualenvs/project/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
TypeError: create() takes exactly 1 argument (5 given)
这里到底出了什么问题?我想我已经正确设置了所有参数,但这仍然失败。
我的代码如下:
class DeveloperManager(models.Manager):
def create_default_developer(self, developer_name, username, password, email):
developer = self.create(developer_name=developer_name, username=username, password=password, email=email)
return developer
class Developer(models.Model):
developer_name = models.CharField(max_length=120)
username = models.CharField(max_length=120)
password = models.CharField(max_length=200)
email = models.EmailField()
billing_email = models.EmailField()
created = models.DateTimeField('date created')
updated = models.DateTimeField('date updated')
@classmethod
def create(cls, developer_name, username, password, email):
developer = cls(developer_name, username, password, email)
developer.save()
return developer
objects = DeveloperManager()
提前感谢您的帮助。
编辑:在developer = Developer(developer_name=developer_name, username=username, password=password, email=email)
中使用create_default_developer()
,它成功创建了对象。但这是否是使用经理的正确方法?如果是的话,我完全可以接受。如果不是,我该怎么做才能正确使用django经理?