Type 'django-admin help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations migrate runserver sendtestemail shell showmigrations sqlflush sqlmigrate sqlsequencereset squashmigrations startapp startproject test testserver Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.)
Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying sessions.0001_initial... OK
D:\django_rest>python manage.py createsuperuser Username (leave blank to use 'shuqing'): 51zxw Email address: 51zxw@163.com Password: Password (again): Superuser created successfully.
启动Server
启动django,然后验证登录我们创建的超级管理员账户。
1 2 3 4 5 6 7 8
D:\django_rest>python manage.py runserver Performing system checks...
System check identified no issues (0 silenced). July 20, 2018 - 16:01:39 Django version 2.0.7, using settings 'django_rest.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
from django.shortcuts import render from django.contrib.auth.models import User,Group from rest_framework import viewsets from api.serializers import UserSerializer,GroupSerializer
from django.contrib import admin from django.urls import path from django.conf.urls import include from rest_framework import routers from api import views
from django.contrib.auth.models import User,Group from rest_framework import viewsets from api.serializers import UserSerializer,GroupSerializer
# Create your views here. classUserViewSet(viewsets.ModelViewSet): """ retrieve: Return a user instance. list: Return all users, ordered by most recently joined. create: Create a new user. delete: Remove an existing user. partial_update: Update one or more fields on an existing user. update: Update a user. """ queryset = User.objects.all() serializer_class = UserSerializer
classGroupViewSet(viewsets.ModelViewSet): """ retrieve: Return a group instance. list: Return all groups, ordered by most recently joined. create: Create a new group. delete: Remove an existing group. partial_update: Update one or more fields on an existing group. update: Update a group. """ queryset = Group.objects.all() serializer_class = GroupSerializer
在urls.py 添加get_schema_view辅助函数
1 2 3 4 5 6 7 8 9 10 11
from rest_framework.schemas import get_schema_view from rest_framework_swagger.renderers import SwaggerUIRenderer,OpenAPIRenderer
deftest_user_already_exists(self): form_data = {'username': 'zxw222', 'email': 'zxw668@qq.com', 'groups': 'http://127.0.0.1:8000/groups/2/'} r = requests.post(self.base_url + '/', data=form_data, auth=self.auth) result = r.json() #预期返回值:{"username":["A user with that username already exists."]} self.assertEqual(result['username'][0], 'A user with that username already exists.')
D:\django_restful>python manage.py test api.tests.UserTest
测试具体的某一条具体用例
1 2
D:\django_restful>python manage.py test api.tests.UserTest.test_get_user
报错相关
1.迁移数据库时没有权限写入
1 2 3
File "C:\Users\jli75\AppData\Local\Programs\Python\Python37\lib\site-packages\MySQLdb\connections.py", line 280, in query _mysql.connection.query(self, query) django.db.utils.InternalError: (7, "Error on rename of '.\\httprunnermanager\\#sql-1178_7.frm' to '.\\httprunnermanager\\djcelery_taskstate.frm' (Errcode: 13 - Permission denied)")
File "C:\Users\jli75\AppData\Local\Programs\Python\Python37\lib\site-packages\MySQLdb\connections.py", line 280, in query _mysql.connection.query(self, query) _mysql_exceptions.OperationalError: (1050, "Table 'djcelery_crontabschedule' already exists")
解决方案:删除migrations文件夹即可。
setting配置错误
1 2 3
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc) django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)' at line 1"))