On Jul 15, 4:19 pm, Jonathan Hayward
<christos.jonathan.hayw...@gmail.com> 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 django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to