Improve RelatedField and callable default handling
- callable default values will now be properly called - PrimaryKeyRelatedField and SlugRelatedField will now return an appropriate type based on the relation model's Field - mock views now have a request object bound even when public is True
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# Generated by Django 2.0 on 2017-12-05 04:05
|
||||
# Generated by Django 2.0 on 2017-12-23 09:07
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
@@ -8,6 +10,7 @@ class Migration(migrations.Migration):
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
@@ -15,12 +18,13 @@ class Migration(migrations.Migration):
|
||||
name='Article',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(help_text='Main article headline', max_length=255, unique=True)),
|
||||
('body', models.TextField(help_text='Article content', max_length=5000)),
|
||||
('slug', models.SlugField(blank=True, help_text='Unique URL slug identifying the article', unique=True)),
|
||||
('title', models.CharField(help_text='title model help_text', max_length=255, unique=True)),
|
||||
('body', models.TextField(help_text='article model help_text', max_length=5000)),
|
||||
('slug', models.SlugField(blank=True, help_text='slug model help_text', unique=True)),
|
||||
('date_created', models.DateTimeField(auto_now_add=True)),
|
||||
('date_modified', models.DateTimeField(auto_now=True)),
|
||||
('cover', models.ImageField(blank=True, upload_to='article/original/')),
|
||||
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='articles', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
# Generated by Django 2.0 on 2017-12-21 15:35
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('articles', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='article',
|
||||
name='body',
|
||||
field=models.TextField(help_text='article model help_text', max_length=5000),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='article',
|
||||
name='slug',
|
||||
field=models.SlugField(blank=True, help_text='slug model help_text', unique=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='article',
|
||||
name='title',
|
||||
field=models.CharField(help_text='title model help_text', max_length=255, unique=True),
|
||||
),
|
||||
]
|
||||
@@ -7,5 +7,6 @@ class Article(models.Model):
|
||||
slug = models.SlugField(help_text="slug model help_text", unique=True, blank=True)
|
||||
date_created = models.DateTimeField(auto_now_add=True)
|
||||
date_modified = models.DateTimeField(auto_now=True)
|
||||
author = models.ForeignKey('auth.User', related_name='articles', on_delete=models.CASCADE)
|
||||
|
||||
cover = models.ImageField(upload_to='article/original/', blank=True)
|
||||
|
||||
@@ -14,12 +14,19 @@ class ArticleSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Article
|
||||
fields = ('title', 'body', 'slug', 'date_created', 'date_modified',
|
||||
fields = ('title', 'author', 'body', 'slug', 'date_created', 'date_modified',
|
||||
'references', 'uuid', 'cover', 'cover_name')
|
||||
read_only_fields = ('date_created', 'date_modified',
|
||||
'references', 'uuid', 'cover_name')
|
||||
lookup_field = 'slug'
|
||||
extra_kwargs = {'body': {'help_text': 'body serializer help_text'}}
|
||||
extra_kwargs = {
|
||||
'body': {'help_text': 'body serializer help_text'},
|
||||
'author': {
|
||||
'default': serializers.CurrentUserDefault(),
|
||||
'help_text': "The ID of the user that created this article; if none is provided, "
|
||||
"defaults to the currently logged in user."
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class ImageUploadSerializer(serializers.Serializer):
|
||||
|
||||
@@ -20,6 +20,11 @@ class NoPagingAutoSchema(SwaggerAutoSchema):
|
||||
return False
|
||||
|
||||
|
||||
class ArticlePagination(LimitOffsetPagination):
|
||||
default_limit = 5
|
||||
max_limit = 25
|
||||
|
||||
|
||||
@method_decorator(name='list', decorator=swagger_auto_schema(
|
||||
operation_description="description from swagger_auto_schema via method_decorator"
|
||||
))
|
||||
@@ -41,8 +46,7 @@ class ArticleViewSet(viewsets.ModelViewSet):
|
||||
lookup_value_regex = r'[a-z0-9]+(?:-[a-z0-9]+)'
|
||||
serializer_class = serializers.ArticleSerializer
|
||||
|
||||
pagination_class = LimitOffsetPagination
|
||||
max_page_size = 5
|
||||
pagination_class = ArticlePagination
|
||||
filter_backends = (DjangoFilterBackend, OrderingFilter)
|
||||
filter_fields = ('title',)
|
||||
ordering_fields = ('date_modified','date_created')
|
||||
|
||||
Reference in New Issue
Block a user