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

[PyMongo] Value from nested JSON object in column_list #1585

Open
nstanke opened this issue Jan 4, 2018 · 2 comments
Open

[PyMongo] Value from nested JSON object in column_list #1585

nstanke opened this issue Jan 4, 2018 · 2 comments

Comments

@nstanke
Copy link

nstanke commented Jan 4, 2018

Hello,

I'm not sure if the following is a bug/not implemented, so please excuse if this doesn't belong here.

Following situation:

I have the following json snippet in my MongoDB:

[{
	"firstname": "Test",
	"surname": "Test",
	"email_address": "example@example.com",
	"country": "Austria",
	"holiday_home": {
		"address": "",
		"address_2": "",
		"city": "",
		"country": "Germany",
		"postal_code": "",
		"state_province": ""
	}
}]

I managed to display the "first level values" (firstname, surname, email, country) in a "standard view" like this without any issues:

class RegistrantView(ModelView):
    column_list = ('firstname', 'surname', 'email_address', 'country')
    form = RegistrantForm

Unfortunately I do not manage to access the key/values stored nested in "holiday_home".

I've tried numerous ways, like column_list = ([holiday_home]['country']) but unfortunately didn't succeed.

Hence I'd like to ask if this is even possible using flask-admin with pymongo.

@julien66
Copy link
Contributor

julien66 commented Mar 18, 2021

Hi,

Stumbling across the exact same issue today.
I'm not knowing either the proper way to achieve that.
I suppose it may be way simpler that what I did (no Flask-Admin expert here) but I decided to post here as it might save time to others...

I just overrided my flask_admin_custom.contrib.pymongo.views
so that :

def _get_field_value(self, model, name): 
  try:
     keys = name.split('.')
     return reduce(dict.get, keys, model)
  except AttributeError:
    return model.get(name) 

instead of just

def _get_field_value(self, model, name):
   return model.get(name) 

It does allow me to add my nested fields columns_list just "naturally-mongo-alike" : "product.client.name"

@julien66
Copy link
Contributor

julien66 commented Mar 18, 2021

Self responding here but since I use Flask Admin more I realised that this need is probably just usually covered by the use of a custom column formatter.

def _client_code_formatter(view, context, model, name):
    mk = "%s (%s)" % (model['product']['client']['name'], model['product']['client']['code'])
    return Markup(mk)

The whole model is accessible from formatters and it's easy to get what you need.

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

No branches or pull requests

2 participants