Starwolf
Starwolf

Reputation: 118

Function to add many to many field in views

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

Upvotes: 0

Views: 38

Answers (1)

PersianMan
PersianMan

Reputation: 964

its simple!

list_of_courses = user.purchased.all()

please read Django documents

Upvotes: 2

Related Questions