title: Django ManyToMany relationship(Movie and Genre) - adding a movie object to a genre
body: <p>I am developing a streaming website. When you upload a movie it gets the genres related to that movie from an API.</p>
  <p>If the genres are not in the Genre table, they are added in the UploadMovie view. When these genres are added to the table, I am adding the Movie those genres are for with on the last line in the code below.</p>
  <p>Example given:
  If I upload the movie: Die Hard 1, which has the following genres: Action, Thriller, those genres are added to the DB, if they doesn't exist already. During the creation of each genre, Die Hard is added to the genre relationship with: genre_to_save.movies.add(Movie.objects.last()) - this should put the movie Die Hard under the genres, action and thriller and it indeed does.
  However when I check the database, I see that Die Hard is also under every other genre that it is NOT, like Fantasy and Science Fiction. Die Hard is movie object(34)</p>
  <p>I am sure that I have some flaw in my function view UploadMovie, but I dont know where.</p>
  <p><a href="https://i.stack.imgur.com/20Vpb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/20Vpb.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/1L3uT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1L3uT.png" alt="enter image description here" /></a></p>
  <pre><code>class UploadMovie(View):
      def get(self, request, *args, **kwargs):
          if not request.user.is_authenticated:
              return redirect('/accounts/login')
          else:
              return render(request, 'core/video_upload.html')
  
      def post(self,request,*args,**kwargs):
          form = MovieForm(request.POST, request.FILES or None)
          
          duplicate_movie = False
          
          print(form.is_valid())
          if form.is_valid():
              print(form.cleaned_data)
              title = form.cleaned_data['title']
              file = form.cleaned_data['file']
              
              movie_id = get_movie_id(title)
              description,backdrop_path,original_title,release_date,runtime,vote_average,tagline,genres = get_movie_details(movie_id)
              
              movie = Movie()
              movie.title = title
              movie.file = file
              movie.description = description
              movie.original_title = original_title
              movie.release_date = release_date
              movie.runtime = runtime
              movie.vote_average = vote_average
              movie.backdrop_path = &quot;https://image.tmdb.org/t/p/original&quot; + backdrop_path
              movie.tagline = tagline
              
              movies = Movie.objects.all()
              db_genres = Genre.objects.all()      
              
              for mov in movies:
                  if mov.original_title == movie.original_title:
                      duplicate_movie == True
                      return redirect('core:profiles')
  
              if not duplicate_movie:
                  movie.save()
              
              print(original_title)
              movie = Movie.objects.last()
  
              print(&quot;Current movie is: &quot; + str(movie.original_title))
              print(&quot;Current movie obj is: &quot; + str(movie))
  
              print(&quot;Genres are the following: &quot; + str(genres))
              
              for genre in genres:
                  if genre not in db_genres:
                      print(&quot;Adding genre: &quot; + genre + &quot; to DB!&quot;)
                      genre_to_save = Genre()
                      genre_to_save.genre = genre
                      genre_to_save.save()
                      print(&quot;Adding &quot; + str(movie.original_title) + &quot; to genre: &quot; + str(genre_to_save.genre))
                      genre_to_save = Genre.objects.last()
                      genre_to_save.movies.add(Movie.objects.last())
                      
              return redirect('core:profiles')
  </code></pre>