我需要创建一个api,其中将包含来自以下两个模型的数据。 我的方法是过滤同一用户的所有活动(开始和结束时间),然后将其与用户详细信息一起传递,其中多个活动(开始和结束时间)将成为唯一用户的列表
class User(models.Model):
u_id = models.CharField(max_length=9,default=None,unique=True)
real_name = models.CharField(max_length=200)
tz = models.CharField(max_length=200)
def __str__(self):
return self.u_id
class Activity(models.Model):
u_id = models.CharField(max_length=9,default=None)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
def __float__(self):
return self.start_time
用户类包含用户详细信息,并且其用户ID是唯一的。 活动类可以包含同一用户ID的多个开始时间和结束时间。
我们需要一个api,其中将包含同一用户的多个开始和结束时间条目。 我尝试将两个模型都查询到一个查询列表中,并将其作为jsonresponse传递,但不起作用。
views.py
def user_activity():
base_url_user = 'http://127.0.0.1:8000/users/'
base_url_activity = 'http://127.0.0.1:8000/activity/'
res_user = requests.get(base_url_user).json()
res_activity = requests.get(base_url_activity).json()
u = {}
a = {}
for i in range(len(res_user)):
u[i + 1] = res_user[i]
for i in range(len(res_activity)):
a[i + 1] = res_activity[i]
for u[i]["u_id"] in a[i]:
querylist = [
{'queryset': User.objects.all()},
{'queryset': Activity.objects.all()}
]
return JsonResponse(querylist)
serializers.py
from rest_framework import serializers
from .models import *
class Userserializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
class Activityserializer(serializers.ModelSerializer):
class Meta:
model = Activity
fields = '__all__'
urls.py
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewset)
router.register(r'activity', views.ActivityViewset)
router.register('ua', views.user_activity, basename = 'ua')
urlpatterns = [
path('',include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
错误
E:\fl_project\fl_assignment\user_activity\views.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "E:\fl_project\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "E:\fl_project\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "E:\fl_project\venv\lib\site-packages\django\core\management\base.py", line 396, in check
databases=databases,
File "E:\fl_project\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "E:\fl_project\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "E:\fl_project\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "E:\fl_project\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "E:\fl_project\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "E:\fl_project\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "E:\fl_project\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "E:\fl_project\venv\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "E:\fl_project\fl_assignment\fl_assignment\urls.py", line 21, in <module>
path('', include('user_activity.urls'))
File "E:\fl_project\venv\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "E:\fl_project\fl_assignment\user_activity\urls.py", line 10, in <module>
path('',include(router.urls)),
File "E:\fl_project\venv\lib\site-packages\rest_framework\routers.py", line 78, in urls
self._urls = self.get_urls()
File "E:\fl_project\venv\lib\site-packages\rest_framework\routers.py", line 339, in get_urls
urls = super().get_urls()
File "E:\fl_project\venv\lib\site-packages\rest_framework\routers.py", line 237, in get_urls
routes = self.get_routes(viewset)
File "E:\fl_project\venv\lib\site-packages\rest_framework\routers.py", line 153, in get_routes
extra_actions = viewset.get_extra_actions()
AttributeError: 'function' object has no attribute 'get_extra_actions'