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
JavaScript
x
14
14
1
# from django.contrib import admin
2
from django.urls import path, include
3
from rest_framework import routers
4
from user_profile.views import UserProfileViewSet, CoursesViewSet
5
6
router = routers.DefaultRouter()
7
router.register(r'user', UserProfileViewSet)
8
router.register(r'courses', CoursesViewSet)
9
10
urlpatterns = [
11
path('', include(router.urls))
12
13
]
14
models.py
JavaScript
1
17
17
1
from django.db import models
2
3
# Creating user profile model.
4
class Courses(models.Model):
5
courses = models.CharField(max_length= 100, blank=True)
6
7
def __unicode__(self):
8
return self.courses
9
10
class UserProfile(models.Model):
11
user_id = models.AutoField(primary_key = True)
12
imgUrl = models.CharField()
13
user_name = models.CharField(max_length=100)
14
user_description = models.TextField()
15
university_college = models.CharField(max_length=100)
16
course = models.ForeignKey(Courses, blank=True, null=True, on_delete=models.CASCADE)
17
views.py
JavaScript
1
17
17
1
from django.shortcuts import render
2
from rest_framework import viewsets
3
from user_profile.models import Courses, UserProfile
4
from user_profile.serializers import UserProfileSerializer, CoursesSerializer
5
# Use these two when you'll create url of one class inside another class
6
# from rest_framework.decorators import action
7
# from rest_framework.response import Response
8
9
10
class UserProfileViewSet(viewsets.ModelViewSet):
11
queryset = UserProfile.objects.all(),
12
serializer_class = UserProfileSerializer
13
14
class CoursesViewSet(viewsets.ModelViewSet):
15
queryset = Courses.objects.all(),
16
serializer_class = CoursesSerializer
17
serializers.py
JavaScript
1
14
14
1
from rest_framework import serializers
2
from user_profile.models import Courses, UserProfile
3
4
class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
5
user_id = serializers.ReadOnlyField()
6
class Meta:
7
model = UserProfile
8
fields = '__all__'
9
10
class CoursesSerializer(serializers.HyperlinkedModelSerializer):
11
class Meta:
12
model = Courses
13
fields = '__all__'
14
Exception log
JavaScript
1
56
56
1
Exception in thread django-main-thread:
2
Traceback (most recent call last):
3
File "C:UserstanmoAppDataLocalProgramsPythonPython310libthreading.py", line 1016, in _bootstrap_inner
4
self.run()
5
File "C:UserstanmoAppDataLocalProgramsPythonPython310libthreading.py", line 953, in run
6
self._target(*self._args, **self._kwargs)
7
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangoutilsautoreload.py", line 64, in wrapper
8
fn(*args, **kwargs)
9
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangocoremanagementcommandsrunserver.py", line 134, in inner_run
10
self.check(display_num_errors=True)
11
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangocoremanagementbase.py", line 475, in check
12
all_issues = checks.run_checks(
13
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangocorechecksregistry.py", line 88, in run_checks
14
new_errors = check(app_configs=app_configs, databases=databases)
15
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangocorechecksurls.py", line 14, in check_url_config
16
return check_resolver(resolver)
17
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangocorechecksurls.py", line 24, in check_resolver
18
return check_method()
19
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangourlsresolvers.py", line 494, in check
20
for pattern in self.url_patterns:
21
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangoutilsfunctional.py", line 57, in __get__
22
res = instance.__dict__[self.name] = self.func(instance)
23
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangourlsresolvers.py", line 715, in url_patterns
24
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
25
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangoutilsfunctional.py", line 57, in __get__
26
res = instance.__dict__[self.name] = self.func(instance)
27
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangourlsresolvers.py", line 708, in urlconf_module
28
return import_module(self.urlconf_name)
29
File "C:UserstanmoAppDataLocalProgramsPythonPython310libimportlib__init__.py", line 126, in import_module
30
return _bootstrap._gcd_import(name[level:], package, level)
31
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
32
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
33
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
34
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
35
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
36
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
37
File "D:WorksDjangoProjectsnote_sharing_application_backendnote_sharing_application_backendurls.py", line 23, in <module>
38
path('api/v1/', include('user_profile.urls')),
39
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesdjangourlsconf.py", line 38, in include
40
urlconf_module = import_module(urlconf_module)
41
File "C:UserstanmoAppDataLocalProgramsPythonPython310libimportlib__init__.py", line 126, in import_module
42
return _bootstrap._gcd_import(name[level:], package, level)
43
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
44
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
45
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
46
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
47
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
48
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
49
File "D:WorksDjangoProjectsnote_sharing_application_backenduser_profileurls.py", line 7, in <module>
50
router.register(r'user', UserProfileViewSet)
51
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesrest_frameworkrouters.py", line 54, in register
52
basename = self.get_default_basename(viewset)
53
File "C:UserstanmoAppDataLocalProgramsPythonPython310libsite-packagesrest_frameworkrouters.py", line 141, in get_default_basename
54
return queryset.model._meta.object_name.lower()
55
AttributeError: 'tuple' object has no attribute 'model'
56
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…
Advertisement
Answer
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:
JavaScript
1
8
1
class UserProfileViewSet(viewsets.ModelViewSet):
2
queryset = UserProfile.objects.all()
3
serializer_class = UserProfileSerializer
4
5
class CoursesViewSet(viewsets.ModelViewSet):
6
queryset = Courses.objects.all()
7
serializer_class = CoursesSerializer
8
Kindly remove them.
Also, in imgUrl
field there must be max_length
in UserProfile
model.