'How can I change/update field value in my User model by button?

I'm using django-cookiecutter User model (added VideoTape ManyToMany connection).

I've created my own model VideoTape. I want to create the option of adding a VideoTape for the user (by updating? videotapes field in User) by clicking the "RENT" button on page VideoTape details:

models.py:

class User(AbstractUser):
    name = CharField(_("Name of User"), blank=True, max_length=255)
    first_name = None  # type: ignore
    last_name = None  # type: ignore
    videotapes = models.ManyToManyField(VideoTape, blank=True, null=True)

    def get_absolute_url(self):
        return reverse("users:detail", kwargs={"username": self.username})
class VideoTape(models.Model):
    title = models.CharField(max_length=256, verbose_name=_("VideoTape Title"))
    slug = models.SlugField(max_length=256, unique=True, editable=False)
    description = models.TextField(verbose_name=_("VideoTape Description"))
    genres = models.TextField(verbose_name=_("VideoTape Genres"), null=True)
    thumbnail = models.URLField(null=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            base = self.title.strip()
            for candidate in generate_slug(base):
                if not VideoTape.objects.filter(slug=candidate).exists():
                    self.slug = candidate
                    break
            else:
                raise Exception("Can't create new VideoTape object")

        super().save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse("videotapes:detail", kwargs={"slug": self.slug})

    def __str__(self):
        return self.title

videotape_detail.html

{% extends "base.html" %}

{% block content %}
{% if request.user.is_superuser %}
<div>
  <a href="{%  url 'videotapes:update' slug=videotape.slug %}"
     class="btn btn-outline-primary">
    <i class="fa fa-edit"></i>Edit
  </a>
  <a href="{%  url 'videotapes:delete' slug=videotape.slug %}" class="btn btn-outline-danger">
    <i class="fa fa-times"></i>Delete
  </a>
</div>
{% endif %}
<div class="card">
  <div class="card-horizontal">
    <div class="img-square-wrapper">
      {% if videotape.thumbnail %}
      <img class=""
           src="{{ videotape.thumbnail }}"
           alt="Card image cap"
           style="height: 700px; width: 100%; display: block;">
      {% else %}
      <img class=""
           src="-"
           alt="Stock card image cap"
           style="height: 700px; width: 100%; display: block;">
      {% endif %}
    </div>
    <div class="card-body">
      <h4 class="card-title"> {{ videotape.title }} </h4>
      <p class="card-text"><b>Description:</b> {{ videotape.description }} </p>
      <p class="card-text"><b>Genres:</b> {{ videotape.genres }} </p>
    </div>
  </div>
</div>
{% endblock %}


Solution 1:[1]

I don't see a rent button? You want to create a form with all details of the videotape. After saving the instance of the form, you can add the newly created videotape object to the user's m2m field.

Read up on forms here: https://docs.djangoproject.com/en/4.0/topics/forms/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dacx