В случае, если вы хотите удалить друга my_friend
из request.user.friends
.
Это будет примерно так:
def delete_friend_request(request, id):
user_profile = request.user.profile
friend_profile = get_object_or_404(Profile,id=id) # Profile instance has the same id as user
user_profile.friends.remove(friend_profile) # A removes B
friend_profile.friends.remove(user_profile) # B removes A
return HttpResponseRedirect('/')
Вам просто нужно удалить его из friends
список, не удаляйте пользовательский экземпляр.
Примечание: Убедитесь, что все User
экземпляры имеют Profile
.
Вы можете обновить текущие значения, используя эту последовательность команд:
python manage.py shell
from django.contrib.auth.models import User
from app_name.models import * # app_name must be the name of your application
user_with_no_profile = User.objects.filter(profile__isnull=True)
for u in user_with_no_profile:
u.profile = Profile()
u.save()
Видя ваш пример, обязательно раскрасьте html следующим образом, чтобы отображался только идентификатор друзей пользователя.
{% for u in request.user.friends.all %}
<a href="{% url 'site:delete_friend_request' u.id %}" class="btn btn-primary btn-lg waves-effect font-weight-bold text-capitalize ml-0 mt-2" style="font-size:14px;padding:10px;width:40%;">Unfriend</a>
{% endfor %}
Отредактировано: Теперь оба друга удалены.