如何通过 AJAX 调用更新 django 中的 M2M 字段
我正在 django 中构建用户配置文件,我希望用户在其中输入他的技能集。技能字段是模型名称 Skills
的 ManyToMany
字段。下面显示了 models.py
文件
class UserProfile(models.Model):
name = models.CharField(max_length = 300, null = True, blank=True)
location = models.CharField(max_length=500, null=True, blank=True)
birthday = models.DateField(null = True, blank = True)
user = models.ForeignKey(User, unique=True)
skills = models.ManyToManyField(Skill, blank=True, null=True)
class Skill(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return u'%s' %(self.name)
,您可以看到所有字段都设置为 null=True
。这是因为我将字段保留为空,并希望用户在他/她想要的时候输入它们。所以我使用 AJAX 调用更新所有这些字段。我已设法编辑所有其他字段,但我不知道如何编辑 M2M 字段
我可以使用 profile.skills 获取链接到配置文件的所有技能的列表。 all()
但我不知道如何更新这个列表。我基本上想从此列表中添加或删除技能对象。我认为 django.db.models.fields.lated.ManyRelatedManager 中有一些东西,我可以使用它来编辑字段
非常感谢任何帮助。我还没有找到任何关于这个主题的东西。有一些有关使用 ModelForm 编辑此字段的信息,但没有有关编辑单个字段的信息。
I am building a user profile in django, where I want the user to enter his skill set. The skill field is a ManyToMany
field to a model name Skills
. Below is shown the models.py
file
class UserProfile(models.Model):
name = models.CharField(max_length = 300, null = True, blank=True)
location = models.CharField(max_length=500, null=True, blank=True)
birthday = models.DateField(null = True, blank = True)
user = models.ForeignKey(User, unique=True)
skills = models.ManyToManyField(Skill, blank=True, null=True)
class Skill(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return u'%s' %(self.name)
As you can see all the fields are set null=True
. This is because I am keeping the fields empty and want the user to input them as and when he/she wants to. So I am updating all these fields using AJAX call. I have managed to edit all the other fields, but I do not know how can I edit a M2M field
I can get a list of all the skills linked to a profile using profile.skills.all()
but I do not know how to update this list. I basically want to add or remove skill objects from this list. I think there is something in the django.db.models.fields.related.ManyRelatedManager
using which I can edit the field
Any help is really appreciated. I have not found anything at all on this subject. There is some information about editing this field using a ModelForm but nothing about editing the individual field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要编辑 m2m 中间表,请使用
ManyRelatedManager
上的add
和remove
方法。https:// docs.djangoproject.com/en/1.3/ref/models/relations/#django.db.models.fields.lated.RelatedManager.add
这是真的,最难的事情google 上的 django 文档有 Manytomanyfield 和其他连续字符串。我撰写有关
formfield_for_manytomany
的博客只是为了出现在我自己的搜索结果中。To edit the m2m intermediary table, use the
add
andremove
methods on theManyRelatedManager
.https://docs.djangoproject.com/en/1.3/ref/models/relations/#django.db.models.fields.related.RelatedManager.add
It's true, the hardest things to google on the django docs are manytomanyfield and other continuous strings. I've blogged about
formfield_for_manytomany
solely to appear in search results for myself.