mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add fields to serializer to resolve source / target refs
This commit is contained in:
parent
ad7e483d8a
commit
7d71a04eaa
@ -696,3 +696,43 @@ def clean_decimal(number):
|
|||||||
return Decimal(0)
|
return Decimal(0)
|
||||||
|
|
||||||
return clean_number.quantize(Decimal(1)) if clean_number == clean_number.to_integral() else clean_number.normalize()
|
return clean_number.quantize(Decimal(1)) if clean_number == clean_number.to_integral() else clean_number.normalize()
|
||||||
|
|
||||||
|
|
||||||
|
def get_objectreference(obj, type_ref: str = 'content_type', object_ref: str = 'object_id'):
|
||||||
|
"""lookup method for the GenericForeignKey fields
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
- obj: object that will be resolved
|
||||||
|
- type_ref: field name for the contenttype field in the model
|
||||||
|
- object_ref: field name for the object id in the model
|
||||||
|
|
||||||
|
Example implementation in the serializer:
|
||||||
|
```
|
||||||
|
target = serializers.SerializerMethodField()
|
||||||
|
def get_target(self, obj):
|
||||||
|
return get_objectreference(obj, 'target_content_type', 'target_object_id')
|
||||||
|
```
|
||||||
|
|
||||||
|
The method name must always be the name of the field prefixed by 'get_'
|
||||||
|
"""
|
||||||
|
model_cls = getattr(obj, type_ref)
|
||||||
|
obj_id = getattr(obj, object_ref)
|
||||||
|
|
||||||
|
# check if references are set -> return nothing if not
|
||||||
|
if model_cls is None or obj_id is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# resolve referenced data into objects
|
||||||
|
model_cls = model_cls.model_class()
|
||||||
|
item = model_cls.objects.get(id=obj_id)
|
||||||
|
url_fnc = getattr(item, 'get_absolute_url', None)
|
||||||
|
|
||||||
|
# create output
|
||||||
|
ret = {}
|
||||||
|
if url_fnc:
|
||||||
|
ret['link'] = url_fnc()
|
||||||
|
return {
|
||||||
|
'name': str(item),
|
||||||
|
'model': str(model_cls._meta.verbose_name),
|
||||||
|
**ret
|
||||||
|
}
|
||||||
|
@ -6,6 +6,7 @@ JSON serializers for common components
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from InvenTree.serializers import InvenTreeModelSerializer
|
from InvenTree.serializers import InvenTreeModelSerializer
|
||||||
|
from InvenTree.helpers import get_objectreference
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
@ -102,7 +103,9 @@ class NotificationMessageSerializer(SettingsSerializer):
|
|||||||
Serializer for the InvenTreeUserSetting model
|
Serializer for the InvenTreeUserSetting model
|
||||||
"""
|
"""
|
||||||
|
|
||||||
#content_object = serializers.PrimaryKeyRelatedField(read_only=True)
|
target = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
source = serializers.SerializerMethodField()
|
||||||
|
|
||||||
user = serializers.PrimaryKeyRelatedField(read_only=True)
|
user = serializers.PrimaryKeyRelatedField(read_only=True)
|
||||||
|
|
||||||
@ -120,11 +123,18 @@ class NotificationMessageSerializer(SettingsSerializer):
|
|||||||
|
|
||||||
read = serializers.BooleanField()
|
read = serializers.BooleanField()
|
||||||
|
|
||||||
|
def get_target(self, obj):
|
||||||
|
return get_objectreference(obj, 'target_content_type', 'target_object_id')
|
||||||
|
|
||||||
|
def get_source(self, obj):
|
||||||
|
return get_objectreference(obj, 'source_content_type', 'source_object_id')
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = NotificationMessage
|
model = NotificationMessage
|
||||||
fields = [
|
fields = [
|
||||||
'pk',
|
'pk',
|
||||||
#'content_object',
|
'target',
|
||||||
|
'source',
|
||||||
'user',
|
'user',
|
||||||
'category',
|
'category',
|
||||||
'name',
|
'name',
|
||||||
|
Loading…
Reference in New Issue
Block a user