class CollectionSerializer(serializers.ModelSerializer):
books = BookSerializer(many=True, source='get_books', read_only=True)
- book_slugs = serializers.CharField(write_only=True)
+ authors = TagSerializer(many=True, read_only=True)
+ book_slugs = serializers.CharField(write_only=True, required=False)
+ author_slugs = serializers.CharField(write_only=True, required=False)
url = AbsoluteURLField()
class Meta:
model = Collection
- fields = ['url', 'books', 'description', 'title', 'book_slugs']
+ fields = [
+ 'url', 'books', 'description', 'title',
+ 'book_slugs', 'authors', 'author_slugs'
+ ]
+
+ def update(self, instance, validated_data):
+ instance = super().update(instance, validated_data)
+ author_slugs = validated_data.get('author_slugs', '').strip().split()
+ if author_slugs:
+ authors = Tag.objects.filter(
+ category='author',
+ slug__in=author_slugs
+ )
+ else:
+ authors = []
+ instance.authors.set(authors)
+ return instance
class FragmentSerializer(serializers.ModelSerializer):
--- /dev/null
+# Generated by Django 4.0.8 on 2022-10-27 09:01
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('catalogue', '0038_auto_20220930_1050'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='collection',
+ name='authors',
+ field=models.ManyToManyField(limit_choices_to={'category': 'author'}, to='catalogue.tag'),
+ ),
+ ]
slug = models.SlugField(_('slug'), max_length=120, primary_key=True)
description = models.TextField(_('description'), null=True, blank=True)
book_slugs = models.TextField(_('book slugs'))
+ authors = models.ManyToManyField(
+ 'Tag',
+ limit_choices_to={'category': 'author'}
+ )
kind = models.CharField(_('kind'), max_length=10, blank=False, default='book', db_index=True,
choices=(('book', _('book')), ('picture', _('picture'))))
listed = models.BooleanField(_('listed'), default=True, db_index=True)