title: I'm unable to accept friend request from other users in my Django social site
body: <p>unable to accept friend requests in my web app
  below are the errors that I'm getting when I'm trying to accept the friend request.</p>
  <blockquote>
  <p>Environment:</p>
  <p>Request Method: GET Request URL:
  <a href="http://127.0.0.1:8000/users/friend-request/accept/5/" rel="nofollow noreferrer">http://127.0.0.1:8000/users/friend-request/accept/5/</a></p>
  <p>Django Version: 2.1 Python Version: 3.8.10 Installed Applications:
  ['users.apps.UsersConfig',  'feed.apps.FeedConfig',  'crispy_forms',
  'stdimage',  'django.contrib.admin',  'django.contrib.auth',
  'django.contrib.contenttypes',  'django.contrib.sessions',
  'django.contrib.messages',  'django.contrib.staticfiles'] Installed
  Middleware: ['django.middleware.security.SecurityMiddleware',
  'django.contrib.sessions.middleware.SessionMiddleware',
  'django.middleware.common.CommonMiddleware',
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
  'django.middleware.clickjacking.XFrameOptionsMiddleware']</p>
  <p>Traceback:</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/backends/utils.py&quot;
  in _execute
  85.                 return self.cursor.execute(sql, params)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py&quot;
  in execute
  296.         return Database.Cursor.execute(self, query, params)</p>
  <p>The above exception (no such table: main.users_profile__old) was the
  direct cause of the following exception:</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/core/handlers/exception.py&quot; in inner
  34.             response = get_response(request)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/core/handlers/base.py&quot;
  in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/core/handlers/base.py&quot;
  in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/contrib/auth/decorators.py&quot; in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)</p>
  <p>File &quot;/home/rohan/ByteWalk-master/users/views.py&quot; in
  accept_friend_request
  88.     user1.profile.friends.add(user2.profile)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py&quot;
  in add
  926.                 self._add_items(self.source_field_name, self.target_field_name, *objs)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py&quot;
  in _add_items
  1088.                     self.through._default_manager.using(db).bulk_create([</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/models/query.py&quot;
  in bulk_create
  465.                 ids = self._batched_insert(objs_without_pk, fields, batch_size)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/models/query.py&quot;
  in _batched_insert
  1152.                 self._insert(item, fields=fields, using=self.db)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/models/query.py&quot;
  in _insert
  1133.         return query.get_compiler(using=using).execute_sql(return_id)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/models/sql/compiler.py&quot;
  in execute_sql
  1285.                 cursor.execute(sql, params)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/backends/utils.py&quot;
  in execute
  100.             return super().execute(sql, params)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/backends/utils.py&quot;
  in execute
  68.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/backends/utils.py&quot;
  in _execute_with_wrappers
  77.         return executor(sql, params, many, context)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/backends/utils.py&quot;
  in _execute
  85.                 return self.cursor.execute(sql, params)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/utils.py&quot;
  in <strong>exit</strong>
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/backends/utils.py&quot;
  in _execute
  85.                 return self.cursor.execute(sql, params)</p>
  <p>File
  &quot;/home/rohan/.local/share/virtualenvs/ByteWalk-master/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py&quot;
  in execute
  296.         return Database.Cursor.execute(self, query, params)</p>
  <p>Exception Type: OperationalError at /users/friend-request/accept/5/
  Exception Value: no such table: main.users_profile__old</p>
  </blockquote>
  <blockquote>
  <p>views.py</p>
  </blockquote>
  <pre><code>import random
  from django.contrib import messages
  from django.contrib.auth import get_user_model
  from django.contrib.auth.decorators import login_required
  from django.http import HttpResponseRedirect
  from django.shortcuts import render, redirect, get_object_or_404
  from feed.models import Post
  from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
  from .models import Profile, FriendRequest
  
  User = get_user_model()
  
  
  @login_required
  def users_list(request):
      users = Profile.objects.exclude(user=request.user)
      sent_friend_requests = FriendRequest.objects.filter(from_user=request.user)
      sent_to = []
      friends = []
      for user in users:
          friend = user.friends.all()
          for f in friend:
              if f in friends:
                  friend = friend.exclude(user=f.user)
          friends += friend
      my_friends = request.user.profile.friends.all()
      for i in my_friends:
          if i in friends:
              friends.remove(i)
      if request.user.profile in friends:
          friends.remove(request.user.profile)
      random_list = random.sample(list(users), min(len(list(users)), 10))
      for r in random_list:
          if r in friends:
              random_list.remove(r)
      friends += random_list
      for i in my_friends:
          if i in friends:
              friends.remove(i)
      for se in sent_friend_requests:
          sent_to.append(se.to_user)
      context = {
          'users': friends,
          'sent': sent_to
      }
      return render(request, &quot;users/users_list.html&quot;, context)
  
  
  def friend_list(request):
      p = request.user.profile
      friends = p.friends.all()
      context = {
          'friends': friends
      }
      return render(request, &quot;users/friend_list.html&quot;, context)
  
  
  @login_required
  def send_friend_request(request, id):
      user = get_object_or_404(User, id=id)
      frequest, created = FriendRequest.objects.get_or_create(
          from_user=request.user,
          to_user=user)
      return HttpResponseRedirect('/users/{}'.format(user.profile.slug))
  
  
  @login_required
  def cancel_friend_request(request, id):
      user = get_object_or_404(User, id=id)
      frequest = FriendRequest.objects.filter(
          from_user=request.user,
          to_user=user).first()
      frequest.delete()
      return HttpResponseRedirect('/users/{}'.format(user.profile.slug))
  
  
  @login_required
  def accept_friend_request(request, id):
      from_user = get_object_or_404(User, id=id)
      frequest = FriendRequest.objects.filter(from_user=from_user, to_user=request.user).first()
      user1 = frequest.to_user
      user2 = from_user
      user1.profile.friends.add(user2.profile)
      user2.profile.friends.add(user1.profile)
      if FriendRequest.objects.filter(from_user=request.user, to_user=from_user).first():
          request_rev = FriendRequest.objects.filter(from_user=request.user, to_user=from_user).first()
          request_rev.delete()
      frequest.delete()
      return HttpResponseRedirect('/users/{}'.format(request.user.profile.slug))
  
  
  @login_required
  def delete_friend_request(request, id):
      from_user = get_object_or_404(User, id=id)
      frequest = FriendRequest.objects.filter(from_user=from_user, to_user=request.user).first()
      frequest.delete()
      return HttpResponseRedirect('/users/{}'.format(request.user.profile.slug))
  
  
  def delete_friend(request, id):
      user_profile = request.user.profile
      friend_profile = get_object_or_404(Profile, id=id)
      user_profile.friends.remove(friend_profile)
      friend_profile.friends.remove(user_profile)
      return HttpResponseRedirect('/users/{}'.format(friend_profile.slug))
  
  
  @login_required
  def profile_view(request, slug):
      p = Profile.objects.filter(slug=slug).first()
      u = p.user
      sent_friend_requests = FriendRequest.objects.filter(from_user=p.user)
      rec_friend_requests = FriendRequest.objects.filter(to_user=p.user)
      user_posts = Post.objects.filter(user_name=u)
  
      friends = p.friends.all()
  
      # is this user our friend
      button_status = 'none'
      if p not in request.user.profile.friends.all():
          button_status = 'not_friend'
  
          # if we have sent him a friend request
          if len(FriendRequest.objects.filter(
                  from_user=request.user).filter(to_user=p.user)) == 1:
              button_status = 'friend_request_sent'
  
          # if we have recieved a friend request
          if len(FriendRequest.objects.filter(
                  from_user=p.user).filter(to_user=request.user)) == 1:
              button_status = 'friend_request_received'
  
      context = {
          'u': u,
          'button_status': button_status,
          'friends_list': friends,
          'sent_friend_requests': sent_friend_requests,
          'rec_friend_requests': rec_friend_requests,
          'post_count': user_posts.count
      }
  
      return render(request, &quot;users/profile.html&quot;, context)
  
  
  def register(request):
      if request.method == 'POST':
          form = UserRegisterForm(request.POST)
          if form.is_valid():
              form.save()
              username = form.cleaned_data.get('username')
              messages.success(request, f'Your account has been created! You can now login!')
              return redirect('login')
      else:
          form = UserRegisterForm()
      return render(request, 'users/register.html', {'form': form})
  
  
  @login_required
  def edit_profile(request):
      if request.method == 'POST':
          u_form = UserUpdateForm(request.POST, instance=request.user)
          p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
          if u_form.is_valid() and p_form.is_valid():
              u_form.save()
              p_form.save()
              messages.success(request, f'Your account has been updated!')
              return redirect('my_profile')
      else:
          u_form = UserUpdateForm(instance=request.user)
          p_form = ProfileUpdateForm(instance=request.user.profile)
      context = {
          'u_form': u_form,
          'p_form': p_form,
      }
      return render(request, 'users/edit_profile.html', context)
  
  
  @login_required
  def my_profile(request):
      p = request.user.profile
      you = p.user
      sent_friend_requests = FriendRequest.objects.filter(from_user=you)
      rec_friend_requests = FriendRequest.objects.filter(to_user=you)
      user_posts = Post.objects.filter(user_name=you)
      friends = p.friends.all()
  
      # is this user our friend
      button_status = 'none'
      if p not in request.user.profile.friends.all():
          button_status = 'not_friend'
  
          # if we have sent him a friend request
          if len(FriendRequest.objects.filter(
                  from_user=request.user).filter(to_user=you)) == 1:
              button_status = 'friend_request_sent'
  
          if len(FriendRequest.objects.filter(
                  from_user=p.user).filter(to_user=request.user)) == 1:
              button_status = 'friend_request_received'
  
      context = {
          'u': you,
          'button_status': button_status,
          'friends_list': friends,
          'sent_friend_requests': sent_friend_requests,
          'rec_friend_requests': rec_friend_requests,
          'post_count': user_posts.count
      }
  
      return render(request, &quot;users/profile.html&quot;, context)
  
  
  @login_required
  def search_users(request):
      query = request.GET.get('q')
      object_list = User.objects.filter(username__icontains=query)
      context = {
          'users': object_list
      }
      return render(request, &quot;users/search_users.html&quot;, context)
  </code></pre>
  <blockquote>
  <p>models.py</p>
  </blockquote>
  <pre><code>class Profile(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE)
      image = models.ImageField(default='default.jpg', upload_to='profile_pics')
      slug = AutoSlugField(populate_from='user')
      bio = models.CharField(max_length=255, blank=True)
      friends = models.ManyToManyField(&quot;Profile&quot;, blank=True)
  
      def __str__(self):
          return str(self.user.username)
  
      def get_absolute_url(self):
          return &quot;/users/{}&quot;.format(self.slug)
  </code></pre>