Home > OS >  how to delete an specific item from a foreign key within .views? Django
how to delete an specific item from a foreign key within .views? Django

Time:03-03

Im working on a Django app where you can join events only under the approval of the owner of the event.

By now, I have the function that adds the current user to the event for approval.

.views

@login_required
def request_event(request, pk):
    previous = request.META.get('HTTP_REFERER')
    try:
        post = Post.objects.get(pk=pk)
        Attending.objects.create(post=post, attendant=request.user)
        messages.success(request, f'Request sent!')
        return redirect(previous)
    except post.DoesNotExist:
        return redirect('/')

and here is the function that deletes de user request (By now is deleting the request of the current logged user)

@login_required
def remove_attendant(request, pk):
    previous = request.META.get('HTTP_REFERER')
    try:
        post = Post.objects.get(pk=pk)
        #attendant = #post.attending_set.all
        Attending.objects.filter(post=post, attendant=request.user).delete()
        messages.success(request, f'User removed!')
        return redirect(previous)
    except post.DoesNotExist:
        return redirect('/')

My situation here is that I'm a having problem to get the attendants of the event (all the users who where added as atendant), so that the owner can reject which ever request he want.

How can I change this so that the function is going to delete a specific user of the event?

Thanks!!

Additional:

models.py

class Attending(models.Model):
    is_approved = models.BooleanField(default=False)
    attendant = models.ForeignKey(User, related_name='events_attending', on_delete=models.CASCADE, null=True)
    post = models.ForeignKey('Post', on_delete=models.CASCADE, null=True)

class Post(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)

urls.py

path('post/<int:pk>/remove_attendant/', views.remove_attendant, name='remove-attendant'),

.html

{% for user in object.attending_set.all %}
                <div >
                      <div >
                        <div  style="height:40px; width:40px;">
                            <img  src="{{ user.attendant.profile.image.url }}">
                        </div>
                      </div>
                      <div >
                        <h6 >{{ user.attendant.first_name }}</h6>
                        <h6 >24 años</h6>
                      </div>
                      <div >
                        <a href="#" style="font-size: 14px !important;" ><i ></i>Approve</a>
                        <a href="   /post/{{ object.pk }}/attendants/{{ object.pk_attendant }}/remove-attendant/" style="font-size: 14px !important;" >Reject</a>
                      </div>


                </div>
{% endfor %}

CodePudding user response:

You need to have the attendant ID in the URL. So, something like posts/<int:pk_post>/attendants/<int:pk_attendant>/remove.

@login_required
def remove_attendant(request, pk_post, pk_attendant):
    previous = request.META.get('HTTP_REFERER')
    try:
        Attending.objects.filter(attendant_id=pk_attendant).delete()
        messages.success(request, f'User removed!')
        return redirect(previous)
    except post.DoesNotExist:
        return redirect('/')

You might notice that you don't even need the post id in the URL. Your choice if you want to keep it. BTW, these thing are explained at great length in the Django tutorial.

  • Related