a) You don't need to use getattr when you know what attr you are
trying to get, those getattr calls can be changed to mgr.filter

b) filter works using kwargs, kwargs work like dictionaries so you can
just create a dictionary and then in the method call do
**dictionary_var to have it called with that dict as the kwargs, for
example:

In [2]: filter = {'title__icontains': 'test'}

In [3]: Page.objects.filter(**filter)
Out[3]: [<Page: This is a test page: I have some content here to
appropriately fill up a test page quite a bit, just need a little
more, >]

In [4]: var = 'title__icontains'

In [5]: filter = {var: 'test'}

In [6]: Page.objects.filter(**filter)
Out[6]: [<Page: This is a test page: I have some content here to
appropriately fill up a test page quite a bit, just need a little
more, >]


On Apr 2, 2:58 pm, Szaijan <[EMAIL PROTECTED]> wrote:
> A follow on question.  In a similar vein, I need to be able to call
> the object manager on models specified at run time.  I figured out how
> to get this to work for cases where the arguments are empty, thanks to
> Malcolm's assstance:
>
> mgr = getattr(eval(modelName), 'objects');
>
> objs = getattr(mgr, 'all')()
>
> However, when trying to use filters or get with arguments, I can't
> figure out how to pass arguments submitted from POST or from another
> method.
>
> objs = getattr(mgr, 'filter')(filterField = filterValue) <-- This
> doesn't work
>
> objs = getattr(mgr, 'filter')("filterField = filterValue") <-- This
> doesn't work
>
> objs = getattr(mgr, 'filter')(filterField, filterValue) <-- This
> doesn't work
>
> objs = getattr(mgr, 'filter')(eval(str(filterField) + "=" +
> str(filterValue)) <-- This doesn't work
>
> Any ideas on how to do this correctly, by which I mean how to pass the
> filter arguments, either directly or as a string?  Thanks.

On Apr 2, 2:58 pm, Szaijan <[EMAIL PROTECTED]> wrote:
> A follow on question.  In a similar vein, I need to be able to call
> the object manager on models specified at run time.  I figured out how
> to get this to work for cases where the arguments are empty, thanks to
> Malcolm's assstance:
>
> mgr = getattr(eval(modelName), 'objects');
>
> objs = getattr(mgr, 'all')()
>
> However, when trying to use filters or get with arguments, I can't
> figure out how to pass arguments submitted from POST or from another
> method.
>
> objs = getattr(mgr, 'filter')(filterField = filterValue) <-- This
> doesn't work
>
> objs = getattr(mgr, 'filter')("filterField = filterValue") <-- This
> doesn't work
>
> objs = getattr(mgr, 'filter')(filterField, filterValue) <-- This
> doesn't work
>
> objs = getattr(mgr, 'filter')(eval(str(filterField) + "=" +
> str(filterValue)) <-- This doesn't work
>
> Any ideas on how to do this correctly, by which I mean how to pass the
> filter arguments, either directly or as a string?  Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to