Home > Enterprise >  Function to add many to many field in views
Function to add many to many field in views

Time:09-13

I want add a course to the users purchased_course. I am unable to figure out how to add it. The Buy Button Link I added on the Course Listview and Detailview I want that particular course to be added in in the users purchased_course which is many to many field.

My models

class Course(models.Model):
...
title = models.CharField(max_length=1000)


class User(AbstractUser):
...
purchased = models.ManyToManyField(Course, blank=True, related_name="buyers")

My views I want the function here

@login_required
def puchases_chaeckout(request):
    user=request.user


    return redirect('student-gome-page')

Please Help

CodePudding user response:

its simple!

list_of_courses = user.purchased.all()

please read Django documents

  • Related