__init}uu()得到一个意外的关键字argumen

2024-04-26 14:25:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个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()

Tags: 模型selfinitmodelsusernamelengthmaxcharfield
1条回答
网友
1楼 · 发布于 2024-04-26 14:25:21

在我看来,这只不过是一个普通的Python问题。您已经从EmbeddedModelField创建了子类,但尚未重写子类中的init方法。因此,当您实例化提供特定于您的子类的参数的类时,这些参数将被直接馈送到基类的init中,然后基类的init就会爆炸。

乍一看Django文档,您将希望重写init并处理特定于类的args/kwargs,并将任何泛型/通用参数传递到基类(下面示例中的文档片段)。

我不是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)

相关问题 更多 >