Django access field's value dynamically by keyword argument to pass to template?

You can use getattr to get the value of an attribute by name. For your example: def edit_topic_text(topicshortname, whichcolumn): thetopic = Topic.objects. Get(topic__shortname__iexact = topicshortname) content = getattr(thetopic, whichcolumn) return render_to_response('topic_text_edit.

Html', locals()) However, you should be also aware of the security implications of this. Users will be able to edit any field on the model they like by changing the url. You should either check the value of whichcolumn before doing anything else with that data, or limit the possibilities in the urlconf with a more specific regular expression, eg: (r'^/(?P\d+)/(?P(Notes|Objectives))/edit/$', 'mysyte.myapp.views.

Edit_topic_text') You also mentioned fields 'Notes' and 'Objectives' but are accessing the field 'objective', so you may need to map the values of whichcolumn to the field name you are interested in, eg: (r'^/(?P\d+)/Objectives/edit/$', 'mysyte.myapp.views. Edit_topic_text', {'whichcolumn': 'objective'}), (r'^/(?P\d+)/Notes/edit/$', 'mysyte.myapp.views. Edit_topic_text', {'whichcolumn': 'note'}) Another thing you should be aware of is that you where accessing the database twice by calling Topic.objects.

Get(...) twice. You should reuse the value of thetopic.

You can use getattr to get the value of an attribute by name. For your example: def edit_topic_text(topicshortname, whichcolumn): thetopic = Topic.objects. Get(topic__shortname__iexact = topicshortname) content = getattr(thetopic, whichcolumn) return render_to_response('topic_text_edit.

Html', locals()) However, you should be also aware of the security implications of this. Users will be able to edit any field on the model they like by changing the url. You should either check the value of whichcolumn before doing anything else with that data, or limit the possibilities in the urlconf with a more specific regular expression, eg: (r'^/(?P\d+)/(?P(Notes|Objectives))/edit/$', 'mysyte.myapp.views.

Edit_topic_text'), You also mentioned fields 'Notes' and 'Objectives' but are accessing the field 'objective', so you may need to map the values of whichcolumn to the field name you are interested in, eg: (r'^/(?P\d+)/Objectives/edit/$', 'mysyte.myapp.views. Edit_topic_text', {'whichcolumn': 'objective'}), (r'^/(?P\d+)/Notes/edit/$', 'mysyte.myapp.views. Edit_topic_text', {'whichcolumn': 'note'}), Another thing you should be aware of is that you where accessing the database twice by calling Topic.objects.

Get(...) twice. You should reuse the value of thetopic.

That worked well for prepopulating the textarea. Thank you. I had read about and tried to fudge getattr(), but hadn't worked out the right syntax.

Unfortunately my save url still doesn't work - I'm struggling to fit the getattr() into the save function, as it's still hardwired as thetopic. Objective = content. Thanks for the security advice, I'll implement that.

Comment also noted about accessing the database twice, thanks. 'Objectives' (plural) was a typo in my post. – nimasmi Jul 1 at 13:32 Update: done it with setattr(object, field, value), thanks to stackoverflow.Com/questions/763558/django-object-get-set-field – nimasmi Jul 1 at 14:18.

You should separate the two concepts of Notes and Objectives in two different classes, then use them in your Topic main class as reference it would be easier for you to retrieve your object type and populate the correct one.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions