Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django: 'int' object has no attribute 'course'

i filter a course using id, i got the profile of the course creator and i want to add the user that purchsed the course the course_creator students, but i keep getting this error
'int' object has no attribute 'course'

views.py

@require_http_methods(['GET', 'POST'])
def payment_response(request, user_course_id=None):
    status=request.GET.get('status', None)
    tx_ref=request.GET.get('tx_ref', None) 
    if status == "successful":
        if user_course_id:
            user_course = UserCourse.objects.filter(id=user_course_id).update(paid=True)

            profile = Profile.objects.get(user=user_course.course.course_creator)
            profile.my_students.add(user_course.user)
            student_profile.my_courses.add(user_course.course)

            student_profile.save()
            profile.save()
            user_course.save()

            return render(request, "payment/payment-success.html")
        else:
            return render(request, "payment/payment-failed.html")
    if status == "cancelled":
        return render(request, "payment/payment-failed.html")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem lies on the line-

user_course = UserCourse.objects.filter(id=user_course_id).update(paid=True)

.update() method returns the number of rows updated after applying the update. Since you are filtering by ID, there is only one course that fits the criteria. After updating, Django just returns 1.

If you want to apply update and then use the item, you can do the following-

course_dict = UserCourse.objects.filter(id=user_course_id)
course_dict.update(paid=True)
user_course = course_dict.first()

or something similar.

Meanwhile, it might happen that the course ID you provided is not existent (maybe course is deleted, or id is wrong). It’d be better to do the necessary checks.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading