Detect type of SlugRelatedField with readonly=True (#83)

* Added test for #82 - readonly SlugRelatedField not showing format uuid
* Added ArticleGroup model
* Added example of SlugRelatedField to a uuid field with and without readonly
* Check remote_field.model in get_related_model

Closes #82.
openapi3
John Carter 2018-03-11 00:29:20 +13:00 committed by Cristi Vîjdea
parent 2c459024d1
commit fb240f6a5b
6 changed files with 67 additions and 3 deletions

View File

@ -9,6 +9,7 @@ Changelog
*Release date: TODO* *Release date: TODO*
- **IMPROVED:** ``serializers.HiddenField`` are now hidden (:issue:`78`) - **IMPROVED:** ``serializers.HiddenField`` are now hidden (:issue:`78`)
- **IMPROVED:** format is now detected for ``SlugRelatedField`` with ``read_only=True`` (:issue:`82`)
********* *********
**1.4.7** **1.4.7**

View File

@ -161,7 +161,11 @@ def get_related_model(model, source):
:return: related model or ``None`` :return: related model or ``None``
""" """
try: try:
return getattr(model, source).rel.related_model descriptor = getattr(model, source)
try:
return descriptor.rel.related_model
except Exception:
return descriptor.field.remote_field.model
except Exception: # pragma: no cover except Exception: # pragma: no cover
return None return None

View File

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.11 on 2018-03-10 01:42
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
import uuid
class Migration(migrations.Migration):
dependencies = [
('articles', '0002_article_article_type'),
]
operations = [
migrations.CreateModel(
name='ArticleGroup',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)),
('title', models.CharField(help_text='title model help_text', max_length=255, unique=True)),
('slug', models.SlugField(blank=True, help_text='slug model help_text', unique=True)),
],
),
migrations.AddField(
model_name='article',
name='group',
field=models.ForeignKey(blank=True, default=None, on_delete=django.db.models.deletion.PROTECT, related_name='articles_as_main', to='articles.ArticleGroup'),
),
migrations.AddField(
model_name='article',
name='original_group',
field=models.ForeignKey(blank=True, default=None, on_delete=django.db.models.deletion.PROTECT, related_name='articles_as_original', to='articles.ArticleGroup'),
),
]

View File

@ -1,3 +1,5 @@
import uuid
from django.db import models from django.db import models
@ -14,3 +16,14 @@ class Article(models.Model):
) )
cover = models.ImageField(upload_to='article/original/', blank=True) cover = models.ImageField(upload_to='article/original/', blank=True)
group = models.ForeignKey('ArticleGroup', related_name='articles_as_main', blank=True, default=None,
on_delete=models.PROTECT)
original_group = models.ForeignKey('ArticleGroup', related_name='articles_as_original', blank=True, default=None,
on_delete=models.PROTECT)
class ArticleGroup(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
title = models.CharField(help_text="title model help_text", max_length=255, blank=False, unique=True)
slug = models.SlugField(help_text="slug model help_text", unique=True, blank=True)

View File

@ -1,7 +1,7 @@
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers from rest_framework import serializers
from articles.models import Article from articles.models import Article, ArticleGroup
class ArticleSerializer(serializers.ModelSerializer): class ArticleSerializer(serializers.ModelSerializer):
@ -12,11 +12,13 @@ class ArticleSerializer(serializers.ModelSerializer):
) )
uuid = serializers.UUIDField(help_text="should articles have UUIDs?", read_only=True) uuid = serializers.UUIDField(help_text="should articles have UUIDs?", read_only=True)
cover_name = serializers.FileField(use_url=False, source='cover', read_only=True) cover_name = serializers.FileField(use_url=False, source='cover', read_only=True)
group = serializers.SlugRelatedField(slug_field='uuid', queryset=ArticleGroup.objects.all())
original_group = serializers.SlugRelatedField(slug_field='uuid', read_only=True)
class Meta: class Meta:
model = Article model = Article
fields = ('title', 'author', 'body', 'slug', 'date_created', 'date_modified', fields = ('title', 'author', 'body', 'slug', 'date_created', 'date_modified',
'references', 'uuid', 'cover', 'cover_name', 'article_type') 'references', 'uuid', 'cover', 'cover_name', 'article_type', 'group', 'original_group', )
read_only_fields = ('date_created', 'date_modified', read_only_fields = ('date_created', 'date_modified',
'references', 'uuid', 'cover_name') 'references', 'uuid', 'cover_name')
lookup_field = 'slug' lookup_field = 'slug'

View File

@ -542,6 +542,7 @@ definitions:
required: required:
- title - title
- body - body
- group
type: object type: object
properties: properties:
title: title:
@ -601,6 +602,13 @@ definitions:
- 3 - 3
- 7 - 7
- 8 - 8
group:
type: string
format: uuid
original_group:
type: string
format: uuid
readOnly: true
Project: Project:
required: required:
- projectName - projectName