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

scope vs not scope example #59

Open
kurtbrose opened this issue Oct 31, 2018 · 1 comment
Open

scope vs not scope example #59

kurtbrose opened this issue Oct 31, 2018 · 1 comment

Comments

@kurtbrose
Copy link
Collaborator

had a real world example of a using-scope vs data structures outside of scope pop up

thought it might be helpful to rework for cookbook or docs -- showing a relatively simple case how both approaches work

def totally_outside_glom():
    models = queryset()
    models_by_id = { model.id: model for model in models}
    values = models.values('id', 'bar')
    for valdict in values:
        valdict['model'] = models_by_id[val_dict[['id']]]
    glom(values,
        [{
            'foo': ('model.foo', T()),
            'bar': 'bar'
        }])


def using_scope():
    models = queryset()
    values = models.values('id', 'bar')
    glom(values,
        [{
            'foo': S['models-by-id'][T['id']].foo(),
            'bar': 'bar',
        }],
        scope={
            'models-by-id': { model.id: model for model in models}
        }
    )

maybe it could be pushed even further down into the scope

@kurtbrose
Copy link
Collaborator Author

neither approach is really perfect, they both have a little bit of rough edges

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

1 participant