title: Creating Group/Teams in Django - Py
body: <p>I am having difficulty finding examples, applications for group creation on Django, and could use some help.</p>
  
  <p>Basically there's a 'Create a New Team' page where the logged in user can use, I am trying to auto-assign the user to the group with <code>.user_set.add(User).</code></p>
  
  <p>My questions are:
  <strong><em>Am I creating the new teams correctly</strong> (when I debug, g1 gets overwritten each time, should I do a For Loop to make sure g+n=? doesn't exist?</em>
  and
  <strong><em>Any way to access the created teams</strong> in the admin page or the Django Py Shell?</em></p>
  
  <p>Thanks in advance, I'm a beginner</p>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>class TeamCreation(models.Model):
      class meta:
          db_table="teams"
  
      team_name = models.CharField(max_length=100)
      team_key = models.CharField(max_length=70)
      date_posted = models.DateTimeField(default=timezone.now)
      author = models.ForeignKey(User, on_delete = models.CASCADE)
  
      def __str__(self):
          return self.team_name
  </code></pre>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>class TeamCreationForm(forms.ModelForm):
      class Meta:
          model = TeamCreation
          fields=['team_name','team_key']
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>from django.shortcuts import render, redirect
  from .forms import TeamCreationForm
  from django.contrib.auth.decorators import login_required
  from django.utils import timezone
  from django.contrib.auth.models import Group
  from .models import TeamCreation
  
  @login_required
  def create_team(request):
  
          if request.POST:
              form = TeamCreationForm(request.POST)
              if form.is_valid():
                  newTeam = form.save(commit=False)
                  newTeam.author = request.user
                  newTeam.date_posted = timezone.now
  
                  newTeam.save()
  
                  #set group name
                  g1 = Group.objects.create(name=newTeam.team_name)
  
                  #autopopulate / assign author to new team
                  g1.user_set.add(newTeam.author)
  
                  #debug (hopefully print: Team Name, Key, Creator, Members
                  print(newTeam)
  
          else:
              form=TeamCreationForm()
  
          context={
                  "form":form,
                  }
  
          return render(request, "pages/team_creation.html", context)
  </code></pre>