How to read \"_id\" field of an object in django templates


/ Published in: Django
Save to your folder(s)

It's imposible to read de "_id" field en django templates ( {{element._id}} === error) So, to read it, you must do a templatetag.

Now you hace the templatetag, you can use it like this: {{element|pk}}


Copy this code and paste it in your HTML
  1. @register.filter(name='pk')
  2. def pk(value):
  3. if type(value) == type({}):
  4. if value.has_key('_id'):
  5. value = value['_id']
  6. return unicode(value)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.