taggedItem.py 792 Bytes
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
2
3
4
5
from backend_app.models.abstract.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend_app.models.tag import Tag
from backend_app.validators.tag import tagged_item_validation
from backend_app.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
    FORCE_FULL_DISPLAY = True
18

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


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