Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describing nested dictionaries followed by dynamic keys for marshal_with #737

Closed
KietnaQuTak opened this issue Oct 17, 2019 · 6 comments
Closed

Comments

@KietnaQuTak
Copy link

KietnaQuTak commented Oct 17, 2019

Hello. I may not understand examples I have found regarding describing model of dictionary with different names as keys cause I have no idea how to describe bellow model properly:

{
	"number": 123,
	"docs": {
		"number_1": {
			"creator": "some@email.pl",
			"subject": "document subject",
			"filename": "document filename",
			"date": "2019-10-16 07:04:50.371130",
			"author": "author name",
			"r1": "recipient 1 name",
			"r2": "recipient 2 name",
			"r3": "recipient 3 name",
			"sent": true
		}
		"number_2": {
			"creator": "some@email.pl",
			"subject": "document subject",
			"filename": "document filename",
			"date": "2019-10-15 07:04:50.371130",
			"author": "author name",
			"r1": "recipient 1 name",
			"r2": "recipient 2 name",
			"r3": "recipient 3 name",
			"sent": true
		}
	}
}

In "docs" I'm planning to change into simple list of objects but for the moment I would like to describe model in which "docs" is a dictionary of smaller dictionaries indicated by keys {"number_1": MODEL_DOC, "number_2": MODEL_DOC}. I was trying to achieve it as bellow but I have now idea how to describe "(document_number)" to indicate that there could be many different document dictionaries followed by document numbers as key (strings).

MODEL_DOC = api.model('Document', {
	CREATOR: fields.String(description='Creator e-mail.'),
	SUBJECT: fields.String(description='Subject.'),
	FILENAME: fields.String(description='Filename.'),
	DATE: fields.DateTime(description='Generation date.'),
	AUTHOR: fields.String(description='Author name.'),
	R1: fields.String(description='1st recipient name.'),
	R2: fields.String(description='2nd recipient name.'),
	R3: fields.String(description='3rd recipient name.'),
	SENT: fields.Boolean(description='Sent status.')
})
MODEL_DOCS = api.model('Documents', {
	'(document number)': fields.Nested(MODEL_DOC)
})

MODEL_DOWNLOAD_RESPONSE = api.model('Document Download Response', {
	NUMBER: fields.Integer(description='Document number.'),
	DOCS: fields.Nested(MODEL_DOCS, description='Dictionary with documents number as key and document data as value.')
})

Best regards
Kietna Qu'Tak

@ziirish
Copy link
Collaborator

ziirish commented Oct 18, 2019

Hello,

What you are looking for is Wildcard fields.
Unfortunately, it does not support Nested inner fields yet (although the PR is actually ready #739).

Details may be found here: #728 (comment)

@ziirish
Copy link
Collaborator

ziirish commented Oct 18, 2019

So, in the end, your model will look like:

MODEL_DOC = api.model('Document', {
	CREATOR: fields.String(description='Creator e-mail.'),
	SUBJECT: fields.String(description='Subject.'),
	FILENAME: fields.String(description='Filename.'),
	DATE: fields.DateTime(description='Generation date.'),
	AUTHOR: fields.String(description='Author name.'),
	R1: fields.String(description='1st recipient name.'),
	R2: fields.String(description='2nd recipient name.'),
	R3: fields.String(description='3rd recipient name.'),
	SENT: fields.Boolean(description='Sent status.')
})
MODEL_NEST = fields.Nested(MODEL_DOC)
MODEL_WILD = fields.Wildcard(MODEL_NEST)
MODEL_DOCS = api.model('Documents', {
	'*': MODEL_WILD
})
MODEL_DOWNLOAD_RESPONSE = api.model('Document Download Response', {
	'number': fields.Integer(description='Document number.'),
	'docs': fields.Nested(MODEL_DOCS, description='Dictionary with documents number as key and document data as value.')
})

Note: it's important to define your MODEL_WILD outside your MODEL_DOCS because we need to keep a track of what objects have already been parsed by the Wildcard field and the best way to do this is to have the Wildcard object registered separately.

@ziirish
Copy link
Collaborator

ziirish commented Nov 14, 2019

Now closing as the patch has been merged

@gabrielbazan
Copy link

Hi all! When will this be released in a new version? It has been merged in November last year, but it's not available in PyPI.
Thanks!

@j5awry
Copy link
Collaborator

j5awry commented Jan 29, 2020

speaking as a maintainer -- flask-restplus has been declared dead. The only person with keys to release has left the project, and didn't hand anything over. There is an official fork at

https://github.com/python-restx/flask-restx

https://pypi.org/project/flask-restx/

it's a drop in replacement, just change your import names

This commit is in the first release of flask-restx (as it was slated for the 0.14 release that never occurred)

@gabrielbazan
Copy link

Thank you very much @j5awry !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants