我試圖構建一個Web服務,但我堅持使用我的模型。我製作了一個Model「User」,它有一個ListField()作爲照片,「Photo」是一個嵌入式文檔。不過,雖然這節省用戶對象,我得到一個錯誤:__init __()得到了一個意想不到的關鍵字參數
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()
嘗試刪除代碼「likedBy = ListField()」 – flycee 2015-04-03 07:41:57
它不會工作我試過了。 – HVT7 2015-04-03 09:13:24