0

I'm a little new to Django, so my question may be basic, but bare with me. Let's say I'm sharing my posts with some of my friends (who are manytomany relation with the post), but also giving an extra condition for whether they can comment on it, or not. This condition, together with the name of the user to be shared with are submitted through a forum. Now my problem is that I don't know how/where to save this condition. I will show you some of my code.

Models.py

class Task(models.Model):
    name = models.CharField(max_length=50)
    text = models.TextField()
    deadline = models.DateTimeField()
    created = models.DateField(auto_now_add=True)
    updated = models.DateField(auto_now=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='taskset')
    shared = models.ManyToManyField(User, blank=True, related_name = 'shared_list')

    def __str__(self):
        return f"{self.name}-{self.user.username}"


class Comment(models.Model):
    text = models.TextField()
    task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='comments')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comments')
    created = models.DateField(auto_now_add=True)

    def __str__(self):
        return f"{self.user.username}-{self.task}"

Share form html

{% extends 'base.html'%}
{% load crispy_forms_tags %}

{% block content %}
<h3>Enter the Username of the Friend you want to share with</h3>
<form method="POST">
    {% csrf_token %}
    {{form|crispy}}
    <input type="submit", value="Share">
</form>
{% endblock content %}

And the view processing it

def share(request, id):
    task = Task.objects.get(id = id)
    if request.method == 'POST':
        share_with = User.objects.get(username = request.POST['username'])
        if share_with is not None:
            task.shared.add(share_with)
            task.save()
        return redirect('index')
    else:
        form = ShareForm()
        return render(request, 'share.html', {'form':form})

Thanks a lot, I've been stuck for two hours, PLEASE HELP!

user1078378
  • 41
  • 1
  • 5
  • Does this answer your question? [Django's ManyToMany Relationship with Additional Fields](https://stackoverflow.com/questions/4443190/djangos-manytomany-relationship-with-additional-fields) – Abdul Aziz Barkat Jun 10 '21 at 15:37
  • It kind of does, but when I add the membership some other parts of my code get messed up for some reason. Specifically, tasks = request.user.task_set.all() friends_tasks = request.user.shared_tasks.all() throw a no such table: main_membership error, which is completely confusing for me – user1078378 Jun 10 '21 at 17:48
  • Your related names are `taskset` (not `task_set`) and `shared_list` (not `shared_tasks`) according to the code you show. Furthermore if you haven't run `makemigrations` and `migrate` then you need to run it. – Abdul Aziz Barkat Jun 10 '21 at 17:56
  • 1
    Dude thank you so much! Your first comment led me into the correct direction, I learned a lost about manytomany additional fields, and on top of that finished the task! Much appreciated – user1078378 Jun 10 '21 at 22:42

0 Answers0