GithubHelp home page GithubHelp logo

Comments (4)

oxan avatar oxan commented on May 24, 2024

No, this currently isn't supported, though I'm not opposed to adding it.

from djangorestframework-dataclasses.

Goutte avatar Goutte commented on May 24, 2024

Docstrings on dataclasses' fields are tricky to read, I'm told.

I managed to get away with adding a 'description' to field()'s metadata and some plumbing in create_field().

@dataclass
class PublisherDto:
	"""
	A Publisher
	"""

	slug: str = field(metadata={
		"description": "Some slug",
		# "serializer_kwargs": {  # this works, but it's verbose
		# 	"help_text": "Some slug",
		# },
	})

class DtoSerializer(DataclassSerializer):

	def create_field(self, field_name: str, extra_kwargs: KWArgs) -> SerializerField:

		# Get the source field (this can be useful to represent a single dataclass field in multiple ways in the
		# serialization state).
		source = extra_kwargs.get('source', '*')
		if source == '*':
			source = field_name

		# Determine the serializer field class and keyword arguments.
		if source in self.dataclass_definition.fields:
			field = self.dataclass_definition.fields[source]


			# HERE ############################
			if 'description' in field.metadata:
				extra_kwargs = {'help_text': field.metadata['description'], **extra_kwargs}

			###################################

			if 'serializer_kwargs' in field.metadata:  # merge extra kwargs defined in the field metadata
				extra_kwargs = {**field.metadata['serializer_kwargs'], **extra_kwargs}

			type_info = field_utils.get_type_info(self.dataclass_definition.field_types[source])
			field_class, field_kwargs = self.build_typed_field(source, type_info, extra_kwargs)
		elif hasattr(self.dataclass_definition.dataclass_type, source):
			field_class, field_kwargs = self.build_property_field(source)
		else:
			field_class, field_kwargs = self.build_unknown_field(source)

		# Include any extra kwargs defined through `Meta.extra_kwargs`
		field_kwargs = self.include_extra_kwargs(field_kwargs, extra_kwargs)

		# Create the serializer field instance.
		return field_class(**field_kwargs)

from djangorestframework-dataclasses.

Goutte avatar Goutte commented on May 24, 2024

You can also override the init to pipe the docstring of the dataclass itself :

class DtoSerializer(DataclassSerializer):
	def __init__(self, *args: Any, **kwargs: Any):
		"""
		Grab the class docstring from the dataclass and set it in the serializer,
		so that the schema generator finds its and uses it.
		"""
		super(DtoSerializer, self).__init__(*args, **kwargs)
		if self.dataclass_definition is not None:
			type(self).__doc__ = self.dataclass_definition.dataclass_type.__doc__

from djangorestframework-dataclasses.

micahjsmith avatar micahjsmith commented on May 24, 2024

This library is super cool but it was not able to meet my needs with ergonomic support for help_text. Easier for me to just manually write out the serializers instead using serializers.Serializer (than use any of the above workarounds for example). The use case is to ensure that the OpenAPI schema generated with drf-spectacular have field descriptions. Thanks all

from djangorestframework-dataclasses.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.