def test_parse_as_slugs_many(self): addon2 = addon_factory() field = SlugOrPrimaryKeyRelatedField(queryset=Addon.objects.all(), many=True) assert field.to_internal_value([self.addon.slug, addon2.slug]) == [ self.addon, addon2, ]
def test_render_as_pks_many(self): obj = Mock() obj.attached = [self.addon] field = SlugOrPrimaryKeyRelatedField(many=True, read_only=True) field.bind('attached', None) assert (field.to_representation(field.get_attribute(obj)) == [self.addon.pk])
def test_render_as_slug(self): obj = Mock() obj.attached = self.addon field = SlugOrPrimaryKeyRelatedField(render_as='slug', read_only=True) field.bind('attached', None) assert (field.to_representation(field.get_attribute(obj)) == self.addon.slug)
class CollectionAddonSerializer(serializers.ModelSerializer): addon = SplitField( # Only used for writes (this is input field), so there are no perf # concerns and we don't use any special caching. SlugOrPrimaryKeyRelatedField(queryset=Addon.objects.public()), AddonSerializer()) notes = TranslationSerializerField(source='comments', required=False) collection = serializers.HiddenField(default=ThisCollectionDefault()) class Meta: model = CollectionAddon fields = ('addon', 'notes', 'collection') validators = [ UniqueTogetherValidator( queryset=CollectionAddon.objects.all(), message=_(u'This add-on already belongs to the collection'), fields=('addon', 'collection')), ] writeable_fields = ('notes', ) read_only_fields = tuple(set(fields) - set(writeable_fields)) def validate(self, data): if self.partial: # addon is read_only but SplitField messes with the initialization. # DRF normally ignores updates to read_only fields, so do the same. data.pop('addon', None) return super(CollectionAddonSerializer, self).validate(data) def to_representation(self, instance): request = self.context.get('request') out = super(CollectionAddonSerializer, self).to_representation(instance) if request and is_gate_active(request, 'collections-downloads-shim'): out['downloads'] = 0 return out
class CollectionAddonSerializer(serializers.ModelSerializer): addon = SplitField( SlugOrPrimaryKeyRelatedField( # .no_cache() because django-cache-machine blows up otherwise. # Only used for writes (this is input field) so no perf concerns. queryset=Addon.objects.public().no_cache()), AddonSerializer()) notes = TranslationSerializerField(source='comments', required=False) collection = serializers.HiddenField(default=ThisCollectionDefault()) class Meta: model = CollectionAddon fields = ('addon', 'downloads', 'notes', 'collection') validators = [ UniqueTogetherValidator( queryset=CollectionAddon.objects.all(), message=_(u'This add-on already belongs to the collection'), fields=('addon', 'collection') ), ] writeable_fields = ( 'notes', ) read_only_fields = tuple(set(fields) - set(writeable_fields)) def validate(self, data): if self.partial: # addon is read_only but SplitField messes with the initialization. # DRF normally ignores updates to read_only fields, so do the same. data.pop('addon') return super(CollectionAddonSerializer, self).validate(data)
def test_render_as_slug(self): obj = Mock() obj.attached = self.addon field = SlugOrPrimaryKeyRelatedField(render_as='slug', read_only=True) field.bind('attached', None) assert field.to_representation(field.get_attribute(obj)) == self.addon.slug
def test_render_as_pks_many(self): obj = Mock() obj.attached = [self.addon] field = SlugOrPrimaryKeyRelatedField(many=True, read_only=True) field.bind('attached', None) assert field.to_representation(field.get_attribute(obj)) == [self.addon.pk]
def test_parse_as_slug(self): field = SlugOrPrimaryKeyRelatedField(queryset=Addon.objects.all()) assert field.to_internal_value(self.addon.slug) == self.addon
def test_parse_as_slugs_many(self): addon2 = addon_factory() field = SlugOrPrimaryKeyRelatedField(queryset=Addon.objects.all(), many=True) assert (field.to_internal_value([self.addon.slug, addon2.slug]) == [self.addon, addon2])