On Oct 27, 10:48 am, James Harrison Fisher <[email protected]>
wrote:
> Hi,
>
> I've recently come across something that's stumped me. I am creating
> a 'shortcut' function for easier creation of add/edit views. This
> function needs to make a query on a model in the simple form:
>
> model.objects.get(field_name=value)
>
> where the variables "model", "field_name", and "value" are passed to
> the function at runtime rather than hard-coded.
>
> This would be fine if I knew that field_name was the primary key, as I
> could just use (pk=value). Problem is, I'm not (necessarily) working
> with a primary key, and I obviously can't pass field_name, which is a
> string, as a the name of a keyword argument. So I would like to do
> something like the following:
>
> def genericView(model, identifier_field, identifier_value):
> model.objects.get(identifierfield, identifier_value)
> [...]
>
> But to the best of my knowledge get() doesn't accept positional
> arguments in such a way, and I can't find a likely method elsewhere
> that might do what I want.
>
> So:
> 1. Do I have a mental block/am I being amazingly stupid? or,
> 2. is there indeed a method to do what I want?, or
> 3. do I have to fall back to SQL?
>
> Thanks in advance,
>
> James
You can do use a dictionary to pass positional arguments, with the **
trick:
model.objects.get(**{identifierfield: identifier_value})
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---