title: Django: How to create a Group through a command?
body: <p>I want to automate all the data creation on my data base, for this I'm using different commands.</p>
  
  <p>I know how to do this for my models, because I know its fields. </p>
  
  <p><strong>But how to do this for Group model?</strong></p>
  
  <pre><code>from django.contrib.auth.models import Group
  </code></pre>
  
  <p>When I query its fields I get: <strong>'user', 'id', 'name', 'permissions'</strong> </p>
  
  <pre><code>[f.name for f in Group._meta.get_fields()]
  </code></pre>
  
  <p>But in Admin Panel I only See that in order to create a Group the <code>name</code> and the <code>permission</code> fields are required: </p>
  
  <p><strong>command</strong>:</p>
  
  <pre><code>import pandas as pd
  from django.contrib.auth.models import Group
  from django.core.management.base import BaseCommand
  
  
  
  tmp_data_groups=pd.read_csv('static/data/groups.csv',sep=',', encoding='iso-8859-1').fillna(" ")
  
  
  
  class Command(BaseCommand):
      def handle(self, **options):
          groups = [
              Group(
                  name=row['name'],
                  permissions=row['permissions']
          )
              for _, row in tmp_data_groups.iterrows()
          ]
  
          Group.objects.bulk_create(groups)
  </code></pre>
  
  <p>I get this error (with the command or in shell):</p>
  
  <blockquote>
    <p>TypeError: Direct assignment to the forward side of a many-to-many set
    is prohibited. Use permissions.set() instead.</p>
  </blockquote>
  
  <p><strong>Example using the shell:</strong> </p>
  
  <p>I've a Group named "Clientes" and it has assigned all the permisions. </p>
  
  <pre><code>Group.objects.all()[0].permissions.all() #Clientes is the only group created
  </code></pre>
  
  <p>I wan to create a new group called "NuevosClientes" (NewClients in english).</p>
  
  <pre><code>Group.objects.create(name='NuevosClientes', permissions='Can view user')
  </code></pre>
  
  <p>Tried this but got:</p>
  
  <pre><code>TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use permissions.set() instead.
  </code></pre>
  
  <p><strong>How should I prepare my CSV field to be read and to generate the desired new group using a command?</strong></p>
  
  <pre><code>| name           | permissions        |   |   |   |
  |----------------|--------------------|---|---|---|
  | NuevosClientes | Can view user      |   |   |   |
  | NuevosClientes | Can add permission |   |   |   |
  | NuevosClientes | Can delete user    |   |   |   |
  | NuevosClientes | Can delete cart    |   |   |   |
  </code></pre>
  
  <p><strong>UPDATE 1</strong>:</p>
  
  <p>I've created my groups, but now I need to create the Permissions to be added to these groups.</p>
  
  <p>But in order to create the Permissions I also need to create the ContentType objects. </p>
  
  <p>How ever, the ContentType object has these arguments.</p>
  
  <pre><code>content_type = ContentType.objects.get(app_label='app_name', model='model_name')
  </code></pre>
  
  <p>According to this asnwer in another post, everytime we create model an entry in the <code>django_content_type</code> table a new record is added in this table for said model. </p>
  
  <blockquote>
    <p>What entry should I use to create the ContentType object?
    What should be the values for app_label='app_name' and model='model_name'?</p>
  </blockquote>
  
  <p>There are a lot of entries in the <code>django_content_type</code> table:</p>
  
  <pre><code>`logentry`, `permission`, `group`, `user`, `contenttype`, `session`, and also for my own models. 
  </code></pre>