@@ -34,6 +34,10 @@ def to_representation(self, obj):
34
34
return str (content_object .resource .ansible_id )
35
35
return None
36
36
37
+ def get_attribute (self , instance ):
38
+ """Override to return the full instance instead of a specific attribute"""
39
+ return instance
40
+
37
41
def to_internal_value (self , value ):
38
42
"""Convert object_ansible_id to object_id for internal use"""
39
43
if not value :
@@ -71,7 +75,7 @@ class BaseAssignmentSerializer(serializers.ModelSerializer):
71
75
content_type = serializers .SlugRelatedField (read_only = True , slug_field = 'api_slug' )
72
76
role_definition = serializers .SlugRelatedField (slug_field = 'name' , queryset = RoleDefinition .objects .all ())
73
77
created_by_ansible_id = ActorAnsibleIdField (source = 'created_by' , required = False , allow_null = True )
74
- object_ansible_id = ObjectAnsibleIdField (source = 'object_id' , required = False , allow_null = True )
78
+ object_ansible_id = ObjectAnsibleIdField (required = False , allow_null = True )
75
79
object_id = serializers .CharField (allow_blank = True , required = False , allow_null = True )
76
80
from_service = serializers .CharField (write_only = True )
77
81
@@ -85,12 +89,16 @@ def validate(self, attrs):
85
89
"""
86
90
rd = attrs ['role_definition' ]
87
91
has_object_id = 'object_id' in attrs and attrs ['object_id' ]
92
+ has_object_ansible_id = 'object_ansible_id' in attrs and attrs ['object_ansible_id' ]
88
93
89
94
if rd .content_type_id :
90
- if not self .partial and not has_object_id :
95
+ if not self .partial and not has_object_id and not has_object_ansible_id :
91
96
raise serializers .ValidationError ("You must provide either 'object_id' or 'object_ansible_id'." )
97
+ # If object_ansible_id was provided and converted, use that for object_id
98
+ if has_object_ansible_id and not has_object_id :
99
+ attrs ['object_id' ] = attrs ['object_ansible_id' ]
92
100
else :
93
- if has_object_id :
101
+ if has_object_id or has_object_ansible_id :
94
102
raise serializers .ValidationError ("Can not provide either 'object_id' or 'object_ansible_id' for system role" )
95
103
96
104
return super ().validate (attrs )
0 commit comments