如何通过 AJAX 调用更新 django 中的 M2M 字段

发布于 2024-12-28 21:18:00 字数 1037 浏览 2 评论 0原文

我正在 django 中构建用户配置文件,我希望用户在其中输入他的技能集。技能字段是模型名称 SkillsManyToMany 字段。下面显示了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝咒 2025-01-04 21:18:00

要编辑 m2m 中间表,请使用 ManyRelatedManager 上的 addremove 方法。

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 and remove methods on the ManyRelatedManager.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文