查询 Django 模板中的多对多字段 [英] Querying Many to many fields in django template

查看:23
本文介绍了查询 Django 模板中的多对多字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能不相关但只是想问,

This may not be relevant but just wanted to ask,

如果对象从视图传递到模板,并且在模板中我将能够查询多对多字段

IF an object is passed from views to template and in the template will i be able to query many to many fields

型号代码:

  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

观看次数:

         // 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 template is it possible to decode the many to many field since we are passing the object

谢谢..

推荐答案

通常,您可以通过 django 模板系统中的路径跟踪任何属性或方法调用而没有参数.

In general, you can follow anything that's an attribute or a method call with no arguments through pathing in the django template system.

对于上面的视图代码,类似

For the view code above, something like

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

应该做你期望的事情.

(我无法从您的代码示例中完全弄清楚细节,但希望这将阐明您可以通过模板获得的内容)

(I couldn't quite make out the specifics from your code sample, but hopefully this will illuminate what you can get into through the templates)

这篇关于查询 Django 模板中的多对多字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆