Create basic API for models
This commit is contained in:
parent
a432535997
commit
2a3823f739
3
authserver/.gitignore
vendored
3
authserver/.gitignore
vendored
|
@ -104,3 +104,6 @@ ENV/
|
|||
|
||||
# DB
|
||||
db.sqlite3
|
||||
|
||||
# media
|
||||
media/*
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
from . import models
|
||||
|
||||
admin.site.register(models.Category)
|
||||
admin.site.register(models.Tool)
|
||||
admin.site.register(models.Profile)
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 2.1.1 on 2018-09-12 21:56
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('api', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='category',
|
||||
name='photo',
|
||||
field=models.FileField(upload_to='static/media/'),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 2.1.1 on 2018-09-12 23:02
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('api', '0002_auto_20180912_2156'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='category',
|
||||
name='photo',
|
||||
field=models.ImageField(upload_to=''),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='tool',
|
||||
name='photo',
|
||||
field=models.ImageField(upload_to=''),
|
||||
),
|
||||
]
|
|
@ -5,14 +5,20 @@ class Category(models.Model):
|
|||
name = models.CharField(max_length=32)
|
||||
slug = models.CharField(max_length=32)
|
||||
info = models.TextField(max_length=1024, blank=True)
|
||||
photo = models.FileField(upload_to='media/')
|
||||
photo = models.ImageField()
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Tool(models.Model):
|
||||
category = models.ForeignKey(Category, related_name='tools', on_delete=models.PROTECT)
|
||||
name = models.CharField(max_length=32)
|
||||
info = models.TextField(max_length=1024, blank=True)
|
||||
wiki_id = models.IntegerField()
|
||||
photo = models.FileField(upload_to='media/')
|
||||
photo = models.ImageField()
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Profile(models.Model):
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
|
|
|
@ -1,7 +1,27 @@
|
|||
from django.contrib.auth.models import User
|
||||
from rest_framework import serializers
|
||||
|
||||
from . import models
|
||||
|
||||
class CategorySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = models.Category
|
||||
fields = '__all__'
|
||||
|
||||
class ToolSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = models.Tool
|
||||
fields = '__all__'
|
||||
|
||||
class ProfileSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = models.Profile
|
||||
fields = ('lockout_admin', 'authorized_tools')
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
profile = ProfileSerializer(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('username', 'email')
|
||||
fields = ('username', 'profile')
|
||||
depth = 1
|
||||
|
|
|
@ -1,7 +1,20 @@
|
|||
from django.contrib.auth.models import User
|
||||
from rest_framework import viewsets
|
||||
from authserver.api.serializers import UserSerializer
|
||||
|
||||
from . import models, serializers
|
||||
|
||||
class CategoryViewSet(viewsets.ModelViewSet):
|
||||
queryset = models.Category.objects.all().order_by('id')
|
||||
serializer_class = serializers.CategorySerializer
|
||||
|
||||
class ToolViewSet(viewsets.ModelViewSet):
|
||||
queryset = models.Tool.objects.all().order_by('id')
|
||||
serializer_class = serializers.ToolSerializer
|
||||
|
||||
class ProfileViewSet(viewsets.ModelViewSet):
|
||||
queryset = models.Profile.objects.all().order_by('-user__date_joined')
|
||||
serializer_class = serializers.ProfileSerializer
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
queryset = User.objects.all().order_by('-date_joined')
|
||||
serializer_class = UserSerializer
|
||||
queryset = models.User.objects.all().order_by('-date_joined')
|
||||
serializer_class = serializers.UserSerializer
|
||||
|
|
|
@ -38,6 +38,7 @@ INSTALLED_APPS = [
|
|||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'rest_framework',
|
||||
'authserver.api',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
@ -119,3 +120,6 @@ USE_TZ = True
|
|||
# https://docs.djangoproject.com/en/2.1/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
||||
MEDIA_URL = '/media/'
|
||||
|
|
|
@ -13,14 +13,19 @@ Including another URLconf
|
|||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.conf import settings
|
||||
from django.conf.urls.static import static
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
|
||||
from django.conf.urls import url, include
|
||||
from rest_framework import routers
|
||||
from authserver.api import views
|
||||
from .api import views
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
router.register(r'tool', views.ToolViewSet, 'tool')
|
||||
router.register(r'category', views.CategoryViewSet, 'category')
|
||||
router.register(r'profile', views.ProfileViewSet, 'profile')
|
||||
router.register(r'user', views.UserViewSet, 'user')
|
||||
|
||||
urlpatterns = [
|
||||
|
@ -28,3 +33,6 @@ urlpatterns = [
|
|||
url(r'^admin/', admin.site.urls),
|
||||
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||
]
|
||||
|
||||
if settings.DEBUG is True:
|
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
|
|
0
authserver/media/.gitkeep
Normal file
0
authserver/media/.gitkeep
Normal file
Loading…
Reference in New Issue
Block a user