I'm new to Django. Please help me to solve this error. I'm not getting any solution. I tried many stackoverflow solutions and GitHub solutions but I'm not getting where the error is coming from.
urls.py
# from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from user_profile.views import UserProfileViewSet, CoursesViewSet
router = routers.DefaultRouter()
router.register(r'user', UserProfileViewSet)
router.register(r'courses', CoursesViewSet)
urlpatterns = [
path('', include(router.urls))
]
models.py
from django.db import models
# Creating user profile model.
class Courses(models.Model):
courses = models.CharField(max_length= 100, blank=True)
def __unicode__(self):
return self.courses
class UserProfile(models.Model):
user_id = models.AutoField(primary_key = True)
imgUrl = models.CharField()
user_name = models.CharField(max_length=100)
user_description = models.TextField()
university_college = models.CharField(max_length=100)
course = models.ForeignKey(Courses, blank=True, null=True, on_delete=models.CASCADE)
views.py
from django.shortcuts import render
from rest_framework import viewsets
from user_profile.models import Courses, UserProfile
from user_profile.serializers import UserProfileSerializer, CoursesSerializer
# Use these two when you'll create url of one class inside another class
# from rest_framework.decorators import action
# from rest_framework.response import Response
class UserProfileViewSet(viewsets.ModelViewSet):
queryset = UserProfile.objects.all(),
serializer_class = UserProfileSerializer
class CoursesViewSet(viewsets.ModelViewSet):
queryset = Courses.objects.all(),
serializer_class = CoursesSerializer
serializers.py
from rest_framework import serializers
from user_profile.models import Courses, UserProfile
class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
user_id = serializers.ReadOnlyField()
class Meta:
model = UserProfile
fields = '__all__'
class CoursesSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Courses
fields = '__all__'
Exception log
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
self.run()
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\runserver.py", line 134, in inner_run
self.check(display_num_errors=True)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 475, in check
all_issues = checks.run_checks(
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
return check_method()
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 494, in check
for pattern in self.url_patterns:
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\functional.py", line 57, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 715, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\functional.py", line 57, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 708, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "D:\Works\DjangoProjects\note_sharing_application_backend\note_sharing_application_backend\urls.py", line 23, in <module>
path('api/v1/', include('user_profile.urls')),
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\conf.py", line 38, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "D:\Works\DjangoProjects\note_sharing_application_backend\user_profile\urls.py", line 7, in <module>
router.register(r'user', UserProfileViewSet)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\routers.py", line 54, in register
basename = self.get_default_basename(viewset)
File "C:\Users\tanmo\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\routers.py", line 141, in get_default_basename
return queryset.model._meta.object_name.lower()
AttributeError: 'tuple' object has no attribute 'model'
Here is the github link of the app - Django
I tried many stackoverflow solutions but I am not understanding from where the error is coming from...
CodePudding user response:
Not sure but I think the issue is with extra commas in both the views, they make it act as a tuple, see it correctly so it should be:
class UserProfileViewSet(viewsets.ModelViewSet):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
class CoursesViewSet(viewsets.ModelViewSet):
queryset = Courses.objects.all()
serializer_class = CoursesSerializer
Kindly remove them.
Also, in imgUrl
field there must be max_length
in UserProfile
model.