【发布时间】:2015-06-18 04:20:39
【问题描述】:
我是 django 的初学者,我现在面临如何将条目添加到 ManyToManyField 的问题。 这是我在 models.py 中的代码:
class UserProfile(models.Model):
# This line is required. Links UserProfile to a User model instance.
user = models.OneToOneField(User)
# The additional attributes we wish to include for a user.
website = models.URLField(blank=True)
picture = models.ImageField(upload_to='profile_images', blank=True)
# Tree field is used to store user's favourite trees
tree = models.ManyToManyField(Tree, blank=True)
def __unicode__(self):
return self.user.username
我想要做的是在我的模板中有一个按钮(例如 tree.html),如果我点击这个按钮,该条目将被添加到用户配置文件树字段中的 ManyToManyField 中。
任何建议将不胜感激!
非常感谢。
【问题讨论】:
-
你试过了吗?
userprofle.tree.add(tree) -
经过身份验证的用户单击按钮并将条目添加到她/他的个人资料中。我理解正确吗?
标签: javascript python django