Querying many of the many fields in a django template

This may not be relevant, but you just need to ask,

IF the object is passed from the views to the template, and in the template I can request many different fields

Model Code:

class Info(models.Model): xls_answer = models.TextField(null=True,blank=True) class Upload(models.Model): access = models.IntegerField() info = models.ManyToManyField(Info) time = models.CharField(max_length=8, null=True,blank=True) error_flag = models.IntegerField() def __unicode__(self): return self.access 

Views:

  // obj_Arr contains all the objects of upload for objs in obj_Arr: logging.debug(objs.access) logging.debug(objs.time) return render_to_response('upload/new_index.html', {'obj_arr': obj_Arr , 'load_flag' : 2}) 

In the template, you can decode the many to many field, as we pass the object

Thanks..

+7
python django django-models django-templates django-views
source share
2 answers

In general, you can ensure that an attribute or method call has no arguments via pathing in the django template system.

For the above view code, something like

 {% for objs in obj_arr %} {% for answer in objs.answers.all %} {{ answer.someattribute }} {% endfor %} {% endfor %} 

should do what you expect.

(I couldn't fully figure out the details from your sample code, but hopefully this will highlight what you can get through the templates)

+27
source share

It is also possible to register a filter as follows:

models.py

 class Profile(models.Model): options=models.ManyToManyField('Option', editable=False) 

extra_tags.py

 @register.filter def does_profile_have_option(profile, option_id): """Returns non zero value if a profile has the option. Usage:: {% if user.profile|does_profile_have_option:option.id %} ... {% endif %} """ return profile.options.filter(id=option_id).count() 

More information about filters can be found here https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

+1
source share

All Articles