简体   繁体   English

如何将新记录添加到多对多字段

[英]how to add a new record to a Many To Many Field

I'm working on a small project using Django / Rest Framework, I have two models ( Contact & List ) I have Many To Many field, in Contact called list.我正在使用 Django / Rest 框架进行一个小项目,我有两个模型(联系人和列表)我有多对多字段,在联系人称为列表。

I would like to know how can I add a record to this relation ( Many To Many Field ).我想知道如何将记录添加到此关系(多对多字段)。

from django.db import models

# Create your models here.

class List(models.Model):
    name = models.CharField(blank=False, max_length=255)
    comment = models.CharField(blank=False, max_length=255)
    private = models.BooleanField(default=False)
    allowed = models.BooleanField(default=False)

    def __str__(self):
        return self.name 

This is my Contact Model这是我的联系人 Model

from django.db import models
from django.conf import settings
from list.models import List


# Create your models here.

class Contact(models.Model):
    # field variables 
    language_choices = (
        ('french', 'french'),
        ('english', 'english'),
    )

    
    """ Relations Between Models """
    list = models.ManyToManyField(List)

I looked for a solution for a long time.我找了很长时间的解决方案。 Here is my method.这是我的方法。

models.py模型.py

class Candidate(models.Model):
    skills = models.ManyToManyField(Skill, through='SkillExperience')

class SkillExperience(models.Model):
    """Skills and experiences for a candidate"""
    skill = models.ForeignKey(Skill, on_delete=models.CASCADE, related_name='skill_to_candidate')
    candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE, related_name='candidate_to_skill')
    experience = models.ForeignKey(Experience, on_delete=models.CASCADE, related_name='experience_to_candidate', default=1)

serializer.py序列化程序.py

class CandidateSkillExperienceSerializer(serializers.ModelSerializer):
    experience = ExperienceSerializer(many=False, allow_null=True)
    skill = SkillSerializerLite(many=False, allow_null=False)

    class Meta:
        model = SkillExperience
        fields = ('skill', 'experience')


class CandidateSerializer(serializers.ModelSerializer):
    candidate_to_skill = CandidateSkillExperienceSerializer(many=True, required=False, allow_null=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过在不同的 model 中创建 function 自动将新记录添加到 One2Many 字段? - How to add new record to One2Many field automatically via create function in different model? 在Django Rest框架中向多对多关系添加新记录 - add new record to many to many relation in django rest framework 通过 Django 中的 api 在多对多字段中添加新项目 - Add new item in many to many field via api in Django 重复相同记录的Many2many字段 - Many2many field with repetition of the same record 如何将数据添加到 Django DRF 中的多对多字段 - How to add data to Many-To-Many field in Django DRF 如何为 Django 中的多对多字段添加值 - How to add value to a many-to-many field in Django 如何解决 ValueError: Invalid field 'example_field_name' on model '_unknown' when creating new record in one2many widget? - How to solve ValueError: Invalid field 'example_field_name' on model '_unknown' when creating new record in one2many widget? 如何保存多对多字段? - How to save many to many field? 如何在 Odoo 10 的 onchange 方法中向 One2many 字段添加一些新记录? - How to add some new records to a One2many field in an onchange method in Odoo 10? Add()方法不能保存多对多字段 - Add() method not saving many to many field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM