title: How to save MantToMany filed attribute in databse?
body: <p>I am making an api in django rest framework. I have created three models in Linkeddata, Individuals group, Event. I have another model Keybase which is another app and have ManyToMany relation with Individual, group and Event. Linked data is in ManyToMany Relation with Individual group and event. While adding data in database,I came to a problem where I dont know How I should save keybase and LinkedData data in Individual, Group and Event table.</p>
  <p>Here is my code:</p>
  <p>models.py</p>
  <pre><code>from django.db import models
  from account_management.models import User
  from django.utils import timezone
  from keybase_management.models import Keybase
  from django.utils.translation import gettext as _
  from django.contrib.postgres.fields import ArrayField
  import datetime
  from .validators import validate_event_date, validate_expiry_date
  # Create your models here.
  
  GENDER = [('male', _('Male')),
            ('female', _('Female')),
            ('other', _('Other'))]
  
  
  class LinkedData(models.Model):
      type = models.CharField(default='', max_length=225)
      query = models.CharField(default='', max_length=225)
      data = models.JSONField(default={'key': 'value'})
  
  
  class Portfolio(models.Model):
  
      class Meta:
          abstract = True
  
      user = models.ForeignKey(User, on_delete=models.CASCADE)
      title = models.CharField(max_length=225, default='')
      created_on = models.DateTimeField(auto_now_add=True)
      updated_on = models.DateTimeField(auto_now=True)
      expire_on = models.DateTimeField(default=timezone.now() + datetime.timedelta(hours=1),
                                       validators=[validate_expiry_date])
      # targets=models.ForeignKey()
      keybase = models.ManyToManyField(Keybase)
      linked_data = models.ManyToManyField(LinkedData)
      addresses = ArrayField(models.CharField(default='null', max_length=225))
      phone_numbers = ArrayField(models.CharField(default='0', max_length=11))
      descriptions = ArrayField(models.CharField(default='null', max_length=225))
  
      def __str__(self):
          return self.title
  
      @staticmethod
      def get_all_portfolios():
          pass
  
      def create_portfolio(self):
          pass
  
      def add_phone_numbers(self):
          pass
  
      def add_addresses(self):
          pass
  
      def add_description(self):
          pass
  
  
  class Individual(Portfolio):
      date_of_birth = models.DateField(default=timezone.now())
      gender = models.CharField(choices=GENDER, max_length=225)
      religion = models.CharField(default='', max_length=225)
      sect = models.CharField(default='', max_length=225)
  
  
  class Group(Portfolio):
      group_type = models.CharField(default='', max_length=225)
      region = models.CharField(default='', max_length=225)
      details = models.CharField(default='', max_length=500)
  
  
  class Event(Portfolio):
      event_date = models.DateTimeField(default=timezone.now() + datetime.timedelta(hours=1),
                                        validators=[validate_event_date])
      event_type = models.CharField(default='', max_length=225)
      location = models.CharField(default='', max_length=225)
      event_details = models.CharField(default='', max_length=500)
  
  </code></pre>
  <p>serializer.py</p>
  <pre><code>from rest_framework import serializers
  from .models import Individual, Group, Event, LinkedData
  from keybase_management.serializers import KeybaseSerializer
  
  
  class LinkedDataSerializer(serializers.ModelSerializer):
      class Meta:
          model = LinkedData
          fields = ('type', 'query', 'data')
  
  
  class IndividualSerializer(serializers.ModelSerializer):
  
      key_base = KeybaseSerializer(read_only=True, many=True)
      linked_data = LinkedDataSerializer(read_only=True, many=True)
  
      class Meta:
          model = Individual
          fields = ('title', 'created_on', 'updated_on', 'expire_on', 'key_base', 'linked_data', 'addresses',
                    'phone_numbers', 'descriptions', 'date_of_birth', 'gender', 'religion', 'sect')
  
  
  class GroupSerializer(serializers.ModelSerializer):
  
      key_base = KeybaseSerializer(read_only=True, many=True)
      linked_data = LinkedDataSerializer(read_only=True, many=True)
  
      class Meta:
          model = Group
          fields = ('title', 'created_on', 'updated_on', 'expire_on', 'key_base', 'linked_data', 'addresses',
                    'phone_numbers', 'descriptions', 'group_type', 'region', 'details')
  
  
  class EventSerializer(serializers.ModelSerializer):
  
      key_base = KeybaseSerializer(read_only=True, many=True)
      linked_data = LinkedDataSerializer(read_only=True, many=True)
  
      class Meta:
          model = Event
          fields = ('title', 'created_on', 'updated_on', 'expire_on', 'key_base', 'linked_data', 'addresses',
                    'phone_numbers', 'descriptions', 'event_date', 'event_type', 'location', 'event_details')
  
  </code></pre>
  <p>view.py</p>
  <pre><code>from django.shortcuts import get_object_or_404, HttpResponse
  from .serializers import LinkedDataSerializer, IndividualSerializer, GroupSerializer, EventSerializer
  from .models import LinkedData, Individual, Group, Event
  from rest_framework import viewsets
  from rest_framework.response import Response
  from keybase_management.models import Keybase
  # Create your views here.
  
  
  class PortfolioViewSet(viewsets.ViewSet):
  
      def list(self, request):
          all_individual_portfolios = Individual.objects.all()
          individual_portfolios_serializer = IndividualSerializer(all_individual_portfolios, many=True)
          all_group_portfolios = Group.objects.all()
          group_portfolio_serializer = GroupSerializer(all_group_portfolios, many=True)
          all_event_portfolios = Event.objects.all()
          event_portfolios_serializer = EventSerializer(all_event_portfolios, many=True)
  
          all_portfolios = {'individual_portfolios': individual_portfolios_serializer.data,
                            'group_portfolios': group_portfolio_serializer.data,
                            'event_portfolios': event_portfolios_serializer.data}
  
          return Response(all_portfolios)
  
      def create(self, request):
          if request.data['individual_portfolios'] is not None:
              for individual_portfolio_data in request.data['individual_portfolios']:
                  title = individual_portfolio_data['title']
                  created_on = individual_portfolio_data['created_on']
                  updated_on = individual_portfolio_data['updated_on']
                  expire_on = individual_portfolio_data['expire_on']
                  addresses = individual_portfolio_data['addresses']
                  phone_numbers = individual_portfolio_data['phone_numbers']
                  descriptions = individual_portfolio_data['descriptions']
                  date_of_birth = individual_portfolio_data['date_of_birth']
                  gender = individual_portfolio_data['gender']
                  religion = individual_portfolio_data['religion']
                  sect = individual_portfolio_data['sect']
  
                  for individual_linked_data in individual_portfolio_data['linked_data']:
                      type = individual_linked_data['type']
                      query = individual_linked_data['query']
                      data = individual_linked_data['data']
  
                      LinkedData.objects.create(type=type, query=query, data=data)
  
                  keybase= Keybase.objects.all()
                  Individual.objects.create(title=title, created_on=created_on, updated_on=updated_on,
                                            expire_on=expire_on,  addresses=addresses, phone_numbers=phone_numbers,
                                            descriptions=descriptions, date_of_birth=date_of_birth, gender=gender,
                                            religion=religion, sect=sect)
  
          if request.data['group_portfolios'] is not None:
              for group_portfolio_data in request.data['group_portfolios']:
                  title = group_portfolio_data['title']
                  created_on = group_portfolio_data['created_on']
                  updated_on = group_portfolio_data['updated_on']
                  expire_on = group_portfolio_data['expire_on']
                  addresses = group_portfolio_data['addresses']
                  phone_numbers = group_portfolio_data['phone_numbers']
                  descriptions = group_portfolio_data['descriptions']
                  group_type = group_portfolio_data['group_type']
                  region = group_portfolio_data['region']
                  details = group_portfolio_data['details']
  
                  if group_portfolio_data['linked_data'] is not None:
                      for group_linked_data in group_portfolio_data['linked_data']:
                          type = group_linked_data['type']
                          query = group_linked_data['query']
                          data = group_linked_data['data']
  
                          LinkedData.objects.create(type=type, query=query, data=data)
  
                  keybase = Keybase.objects.all()
                  keybase = Keybase.objects.all()
                  Individual.keybase.add(keybase)
                  Group.objects.create(title=title, created_on=created_on, updated_on=updated_on,
                                       expire_on=expire_on, addresses=addresses, phone_numbers=phone_numbers, descriptions=descriptions,
                                       group_type=group_type, region=region, details=details)
  
  
  
          if request.data['event_portfolios'] is not None:
              for event_portfolio_data in request.data['event_portfolios']:
                  title = event_portfolio_data['title']
                  created_on = event_portfolio_data['created_on']
                  updated_on = event_portfolio_data['updated_on']
                  expire_on = event_portfolio_data['expire_on']
                  addresses = event_portfolio_data['addresses']
                  phone_numbers = event_portfolio_data['phone_numbers']
                  descriptions = event_portfolio_data['descriptions']
                  event_date = event_portfolio_data['event_date']
                  event_type = event_portfolio_data['event_type']
                  location = event_portfolio_data['location']
                  event_details = event_portfolio_data['event_details']
  
                  if event_portfolio_data['linked_data'] is not None:
                      for event_linked_data in event_portfolio_data['linked_data']:
                          type = event_linked_data['type']
                          query = event_linked_data['query']
                          data = event_linked_data['data']
  
  
                          LinkedData.objects.create(type=type, query=query, data=data)
  
  
                  Event.objects.create(title=title, created_on=created_on, updated_on=updated_on, expire_on=expire_on,
                                       addresses=addresses, phone_numbers=phone_numbers, descriptions=descriptions,
                                       event_date=event_date, event_type=event_type, location=location, event_details=event_details)
  
  
  </code></pre>
  <p>urls.py</p>
  <pre><code>from django.urls import path
  from .views import PortfolioViewSet
  from rest_framework.routers import DefaultRouter
  app_name = 'portfolio_management'
  
  # router = DefaultRouter()
  # router.register('/portfolio_viewset', PortfolioViewSet, basename='')
  
  urlpatterns = [
          path('portfolio_list/', PortfolioViewSet.as_view({'get': 'list'}), name='portfolio_list'),
          path('portfolio_create/', PortfolioViewSet.as_view({'post': 'create'}), name='portfolio_create')
  
  ]
  
  </code></pre>