taggedItem.py 746 Bytes
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.db import models
2 3
from backend.models.abstract.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.models.tag import Tag
4
from backend.validators.tag import tagged_item_validation
5
from backend.fields import JSONField
Florent Chehab's avatar
Florent Chehab committed
6 7


8
class TaggedItem(BasicModule):
Florent Chehab's avatar
Florent Chehab committed
9
    tag = models.ForeignKey(Tag, related_name='+', on_delete=models.PROTECT)
Florent Chehab's avatar
Florent Chehab committed
10
    custom_content = JSONField(default=dict)
Florent Chehab's avatar
Florent Chehab committed
11 12 13 14 15

    class Meta:
        abstract = True


16
class TaggedItemSerializer(BasicModuleSerializer):
17

Florent Chehab's avatar
Florent Chehab committed
18
    def my_validate(self, attrs):
19 20
        tagged_item_validation(attrs)
        return attrs
Florent Chehab's avatar
Florent Chehab committed
21 22


23
class TaggedItemViewSet(BasicModuleViewSet):
24
    def extend_queryset(self):
25
        return self.my_model_queryset.prefetch_related('tag')