'tuple' object has no attribute 'is_preview'

i am woking on a project where users can create a videos and check if the particular can be previewed before they purchase or not, everything was working fine up till now i got this 'tuple' object has no attribute 'is_preview' i can’t really tell where this error is coming from.

This is where the traceback is pointing too

Traceback (most recent call last):
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\Destiny\Desktop\DexxaEd\dnextedprj\course\views.py", line 122, in course_details
    if(video.is_preview is False):
AttributeError: 'tuple' object has no attribute 'is_preview'
[01/May/2022 20:08:43] "GET /course/et-ea-velit-ad-veritatis-explicabo HTTP/1.1" 500 73905

views.py line 122

    if serial_number is None:
        serial_number = 1

    # if Video.objects.all().exists
    video = Video.objects.get_or_create(serial_number=serial_number, course=course)
    video_count = Video.objects.get(serial_number=serial_number, course=course)

    if(video.is_preview is False):

        if request.user.is_authenticated is False:
            return redirect("course:sign-in")
            user = request.user
                user_course = UserCourse.objects.get(user=user, course=course)
                return redirect("course:check-out", slug=course.slug)


CourseVideosFormset = inlineformset_factory(Course, Video, fields=('title','serial_number', 'video_id', 'is_preview'))

>Solution :

.get_or_create(…) [Django-doc] returns a 2-tuple with the object created/retrieved as first item, and a boolean that is True in case the object was created as second. You thus unpack these with:

video, __ = Video.objects.get_or_create(serial_number=serial_number, 

Leave a Reply