You could use Django's content types, too.

from django.contrib.content_types.models import ContentType
my_ct = ContentType.objects.get(app_label='my_app', model='MyModel')
# ...or you could leave off app_label, if there are no conflicting model
names
my_ct.get_object_for_this_type(pk=my_pk)

That way you could use get_object_or_404() in your view.

Ben

On Thu, Jul 15, 2010 at 10:21 AM, Jonathan Hayward <
[email protected]> wrote:

> Thank you! Let me play with it.
>
>
> On Thu, Jul 15, 2010 at 10:41 AM, Daniel Roseman <[email protected]>wrote:
>
>> On Jul 15, 4:19 pm, Jonathan Hayward
>> <[email protected]> wrote:
>> > I'm looking at a problem and see how to solve it, probably badly, with
>> > eval(), but don't see what the right solution is.
>> >
>> > I want to use Jeditable more or less to make fields on models editable.
>> > Jeditable sends the HTML ID, as well as updated value, when someone
>> makes an
>> > in_place edit. I am following a convention where the HTML ID is
>> something
>> > like *model*_*field*_*id*, and I would like an Ajax view to do some
>> error
>> > checking and if the provided ID is appropriate, take an ID of
>> > entity_description_1 to pull up the Entity with id 1 and set its
>> description
>> > field to the updated value.
>> >
>> > I see a way to do this with eval, badly, but what I'd guess of Django is
>> > that there's a way to call .get(model_name) or something like that to
>> avoid
>> > an eval. Is there a dynamic equivalent to
>> > directory.models.Entity.objects.get(pk = 1) or instance.description =
>> value
>> > where "Entity" and "description" are effectively replaced by dynamically
>> > provided values?
>> >
>>
>> Yes - although you need the name of the app as well as the model.
>>
>>    from django.db.models import get_model
>>    my_model = get_model('myapp', 'Entity')
>>    my_instance = my_model.objects.get(pk=my_pk_value)
>>    setattr(my_instance, fieldname, fieldvalue)
>>    my_instance.save()
>>
>> --
>> 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]<django-users%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>>
>
>
> --
> → Jonathan Hayward, [email protected]
> → An Orthodox Christian author: theology, literature, et cetera.
> → My award-winning collection is available for free reading online:
> ☩ I invite you to visit my main site at http://JonathansCorner.com/
>
> --
> 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]<django-users%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
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.

Reply via email to