Django Serializers有很多关系 [英] Django Serializers many to many relationship

查看:308
本文介绍了Django Serializers有很多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angularjs和Django进行一个小项目。我有两个不同的模型及其相应的终点。



I am working in a small project using Angularjs and Django. I have two different models with its corresponding end points.

class Skills(models.Model):
    skills = models.CharField(max_length=100)

    def __str__(self):
        return self.skills



  class Project(models.Model):
...
    required_skills = models.ManyToManyField(
            Skills, verbose_name="list of skills")
..





这是一个我的技能终点副本





This is a copy of my skills end point

[
    {
        "id": 1,
        "skills": "Css"
    },
    {
        "id": 2,
        "skills": "HTML5"
    },
    {
        "id": 3,
        "skills": "Java"
    }
]





在首页我有一个使用$ http.post服务将数据发送到数据库的表单。当我创建项目时,它可能需要一种技能或多种技能。这是我的困惑来了,我可以通过发送pk来存储我的外键,在这种情况下是pk。但是,由于我处理的是多对多的关系,我无法弄清楚什么是最好的方法。我已经阅读了文档和一些在线资源,但都取得了成功。此外,表单有一个下拉菜单,其中包含技能列表。我使用$ http.get获取此值。请问有谁知道如何解决这个问题?



On the front page I have a form that uses $http.post service to send data to the database. When I project is created, it might require one skill or multiple skills. Here is where my confusion come, I can store I foreign key by sending the pk which in this case is id. However, since I am dealing with a many to many relationship, I can not figure out what is the best way to proceed. I have read the documentation and some online resources with any success. Also, the form has an dropdown menu with the list of skills.I got this values using $http.get. Please anyone has any idea of how to solve this issue?

推荐答案

http.post服务将数据发送到数据库。当我创建项目时,它可能需要一种技能或多种技能。这是我的困惑来了,我可以通过发送pk来存储我的外键,在这种情况下是pk。但是,由于我处理的是多对多的关系,我无法弄清楚什么是最好的方法。我已经阅读了文档和一些在线资源,但都取得了成功。此外,表单还有一个带有技能列表的下拉菜单。我使用
http.post service to send data to the database. When I project is created, it might require one skill or multiple skills. Here is where my confusion come, I can store I foreign key by sending the pk which in this case is id. However, since I am dealing with a many to many relationship, I can not figure out what is the best way to proceed. I have read the documentation and some online resources with any success. Also, the form has an dropdown menu with the list of skills.I got this values using


http.get获得了这个值。请问有谁知道如何解决这个问题?
http.get. Please anyone has any idea of how to solve this issue?


这篇关于Django Serializers有很多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆