gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:45:59 34 4
gpt4 key购买 nike

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

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 = 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 继承了子类,但尚未重写子类中的 init 方法。因此,当您实例化该类并提供特定于您的子类的参数时,这些参数将直接提供给基类​​的 init,然后基类的 init 就会被轰炸。

浏览一下 Django 文档,您将需要重写 init 并处理特定于类的参数/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)

关于python - __init__() 得到了意外的关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29428078/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com