__init__() 得到了意外的关键字参数

__init__() got an unexpected keyword argument

我正在尝试构建 Web 服务,但我受困于我的模型。我制作了一个模型 "User",它有一个 ListField() 作为照片,"Photo" 是一个嵌入式文档。但是在保存这个用户对象时我得到一个错误:

Traceback (most recent call last):
File "E:\Challenge\trial\services\workspace\Service\src\appservices\trial.py", 
  line 7, in <module>
  likedBy=["Name1", "Name2", "Name3", "Name4"]))
File "E:\Challenge\trial\Python27\lib\site-packages\djangotoolbox\fields.py", 
    line 253, in __init__
    super(EmbeddedModelField, self).__init__(*args, **kwargs)
    TypeError: __init__() got an unexpected keyword argument 'likedBy'

下面是我的模型文件:

from django.db import models
from djangotoolbox.fields import ListField, EmbeddedModelField

class User(models.Model):
    username = models.CharField(max_length=100, blank=False, unique = True)
    fname = models.CharField(max_length=100, blank=False)
    lname = models.CharField(max_length=100, blank=True)
    photos = ListField()        #embedded list of photos uploaded by users
    created = models.DateTimeField(auto_now_add=True)    

    def __unicode__(self):
        return self.name

class Photo(EmbeddedModelField):
    description = models.TextField()
    link = models.TextField()
    like = models.IntegerField
    likedBy = ListField()

    def __unicode__(self):
        return self.name

我尝试保存用户对象的方式是:

user = User(username="username", fname="Harshal", lname="Tripathi")
user.photos.append(Photo(description="This is a great photo uploaded for trial", link="http://image.com/images/user_photo.jpg", like="365", likedBy=["Name1", "Name2", "Name3", "Name4"]))
user.save()

在我看来,这只不过是一个正常的 Python 问题。您已经从 EmbeddedModelField 中进行了 subclassed,但是您还没有覆盖 subclass 中的 init 方法。因此,当您实例化 class 提供特定于您的子 class 的参数时,这些参数将直接馈送到基础 class 的 init,然后被轰炸了。

看一眼 Django 文档,您会想要覆盖 init 并处理您的 class-specific args/kwargs 并传递任何 generic/common 参数直到基础 class(来自以下示例下方文档的片段)。

我不是 Django 开发人员,没有时间安装和设置它,但根据您上面提供的代码,我希望以下内容能够正常工作,除非我是 Django 固有的东西不了解也不会在文档中一目了然。

from django.db import models
from djangotoolbox.fields import ListField, EmbeddedModelField

class User(models.Model):
    username = models.CharField(max_length=100, blank=False, unique = True)
    fname = models.CharField(max_length=100, blank=False)
    lname = models.CharField(max_length=100, blank=True)
    photos = ListField()        #embedded list of photos uploaded by users
    created = models.DateTimeField(auto_now_add=True)    

    def __unicode__(self):
        return self.name

class Photo(EmbeddedModelField):
    description = models.TextField()
    link = models.TextField()
    like = models.IntegerField
    likedBy = ListField()

    def __init__(self, link=None, like=None, likedBy=None, *args, **kwargs):
        super(Photo, self).__init__(*args, **kwargs)
        self.link = link or self.link
        self.like = like or self.like
        self.likedBy = likedBy or self.likedBy

    def __unicode__(self):
        return self.name

Writing a field subclass¶

When planning your Field subclass, first give some thought to which existing Field class your new field is most similar to. Can you subclass an existing Django field and save yourself some work? If not, you should subclass the Field class, from which everything is descended.

Initializing your new field is a matter of separating out any arguments that are specific to your case from the common arguments and passing the latter to the __init__() method of Field (or your parent class).

In our example, we’ll call our field HandField. (It’s a good idea to call your Field subclass Field, so it’s easily identifiable as a Field subclass.) It doesn’t behave like any existing field, so we’ll subclass directly from Field:

from django.db import models

class HandField(models.Field):

    description = "A hand of cards (bridge style)"

    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 104
        super(HandField, self).__init__(*args, **kwargs)