1

I want to build a website in which users can send words to each other using Django. When a user deletes a word, that word will be deleted for only that user. I have a working website; the only problem is when a word is deleted, it is deleted for all users. How can I fix this problem, is it a problem which is related to ManyToMany relationships?

Django word model:

from django.db import models
from datetime import datetime
from django.contrib.auth.models import User


class Word(models.Model):
   definition = models.CharField(max_length=350)
   turkish = models.CharField(max_length=50)
   english = models.CharField(max_length=50)
   users = models.ManyToManyField(User)
   creator = models.CharField(max_length=50)

   def __str__(self):
      return self.english

   def summary(self):
      return self.definition[:50] + "..."

The share view function:

@login_required
def send(request):
    users_list = User.objects.all().exclude(username=request.user.username)
    user = request.user
    small = user.username.title()
    send_to_str = request.POST['user']
    sent_word_str = request.POST['word']
    send_to = User.objects.get(username=send_to_str)
    sent_word = Word.objects.get(id=sent_word_str)
    if not users_list:
         sendmessage = 'You have no friends'
    else:
         sendmessage = ''
    sent_word.users.add(send_to)
    words = Word.objects.filter(users=user).order_by('-english')

    return render(request, 'intro.html', {'sendmessage': sendmessage, 'words': words, 'small' : small})

The delete view function:

@login_required
def delete(request):
    if request.method == 'POST':
        current_id = request.POST['word_id']
        current_word = Word.objects.get(id=current_id)
        current_word.delete()
        messages.add_message(request, messages.INFO, 'Succesfully deleted.')
        return redirect('translater:home')

    else:
        return render(request, 'intro.html', {'message': 'there is a problem'})

Redirected to this html:

  {% if words %}
  <h3 style="color:gray;font-weight:bold">My Word List</h3>
  <br>
  {% endif %}

  {% for word in words %}

  <h4 style="color:red">{{ word.english }}&nbsp;/&nbsp;{{ word.turkish }} </h4>
  <h4 style="color:green">{{ word.summary }}&nbsp;/&nbsp;{{ word.creator }}</h4>

  <br>


  <button type="button" onClick="document.getElementById('deleterForm').submit()" class="btn btn-warning">Delete!</button>
  <button type="button" onClick="document.getElementById('sharerForm').submit()" class="btn btn-primary">Share!!</button>

  <form id="deleterForm" action="{% url 'translater:delete' %}" method="POST">
      {% csrf_token %}
      <input type="hidden" name="word_id" value="{{ word.id }}">
  </form>

  <br>

  <form id="sharerForm" action="{% url 'translater:share' %}" method="POST">
      {% csrf_token %}
      <input type="hidden" name="word_id" value="{{ word.id }}">
  </form>

  <form id="senderForm{{ word.id }}" action="{% url 'translater:send' %}" method="POST">
      {% csrf_token %}

      {% for user in users_list %}
        <input type="submit" name="user" value="{{ user }}">
        <input type="hidden" name="word" value="{{ word.id }}">
      {% endfor %}
      {{ sendmessage }}
  </form>

  <br>
  {% endfor %}
Göktuğ Aşçı
  • 471
  • 6
  • 15
  • you are trying to actually delete the word, but instead, your "delete" should just update the user column. It's not actually deleting, it's just an update that only affects the "user" field.. just like any other "update" – morinx Apr 23 '17 at 13:59
  • how can I pick the word given to user? current_english = request.POST['word_english'] current_word = user.words.get(english=current_english) – Göktuğ Aşçı Apr 23 '17 at 14:14
  • you don't pick the word from user. forget about the user. Just think of it as a simple change in many to many relationship. You just want to remove 'request.user' from the m2m table. see this answer on how to simply update m2m table: http://stackoverflow.com/a/1195768/3802821 – morinx Apr 23 '17 at 14:23
  • Thank you for your answer :D – Göktuğ Aşçı Apr 23 '17 at 19:49

0 Answers0