从 for 循环模板 Django 上的多对多字段访问 date_subscribed
Access date_subscribed from a many to many field on a for loop template Django
我有这些模型,
class Place(models.Model):
name = models.CharField(max_length=200)
user = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='places', default=1)
subscribers = models.ManyToManyField(AppUser, through='PlaceSubscriber')
def __str__(self): # __unicode__ on Python
return self.name
class PlaceSubscriber(models.Model):
place = models.ForeignKey(Place, on_delete=models.CASCADE)
user = models.ForeignKey(AppUser, on_delete=models.CASCADE)
date_subscribed = models.DateTimeField(editable=False, default=timezone.now)
class Meta:
unique_together = ('place', 'user')
我想访问模板中此 for 循环的 date_subscribed 字段
{% for o in place.subscribers.all %}
<a href="#" class="list-group-item clearfix">
<span class="pull-left">
{{ forloop.counter }}.  
</span>
<span class="pull-left">
<strong>{{ o.full_name }}</strong>
<p>Email: <i>{{ o.email }}</i> | Date Subscribed: <i> {{
o.place__placesubscriber__date_subscribed }} </i> </p>
</span>
<span class="pull-right">
<span class="btn btn-xs btn-primary"
onclick="sendPushNotificationToUser('{{ o.ionic_id }}'); return
false;">Send Message</span>
<span class="btn btn-xs btn-danger" onclick="deletePlaceUser({{ place.id
}}, {{ o.id }}); return false; ">
Unsubscribe
</span>
</button>
</span>
</a>
{% endfor %}
我可以像这样访问这个 foorloop 之外的 date_subscribed 字段:
{% for each in place.placesubscriber_set.all %}
{{ each.date_subscribed }}
{% endfor %}
但还没有弄清楚如何在另一个内部访问它。
更新
这是我的看法
class PlaceDetailView(DetailView):
model = Place
template_name = 'place/place_detail.html'
这是 url 模式
url(r'^place/(?P<pk>\d+)$', views.PlaceDetailView.as_view(), name='detail_place'),
我认为在你的 views.py 文件中你应该这样做:
from django.shortcuts import render
from .models import Place
def placeview(request):
place = Place()
return render(request, 'place.html', {'place':place})
我通常会尝试在我的模板中进行良好的查询来处理简单的数据结构,避免复杂的逻辑。
我会将视图替换为一个简单的视图,它可以进行您想要的查询(希望如此):
def place_detail(request):
data = PlaceSubscriber.objects.all().values(
'user__full_name',
'user__email',
'place__name',
'date_subscribed'
)
return render(request,'place.html',{'data':data})
这会生成一个包含查询键的字典列表。您现在可以通过以下方式显示此数据:
{% for el in data %}
{el.user__full_name}
{el.date_subscribed}
...
{% endfor %}
嗯,我不确定你是否想要,但你可以更改查询值以获得你想要的。
希望对您有所帮助。
我最终采用了@Tico 方法,我更新了我的视图,因此逻辑比以前少了,这是我的更新:
更新了我的 urls.py 文件,停止使用通用详细信息视图 Class
url(r'^place/(?P<place_id>\d+)$', views.place_detail, name='detail_place'),
将 views.py 文件的详细视图方法更新为 return 我需要的两个模型的数据:
def place_detail(request, place_id):
place = get_object_or_404(Place, pk=place_id)
placeSubscribers = PlaceSubscriber.objects.all().filter(place=place).values(
'user__full_name',
'user__email',
'user__ionic_id',
'place__name',
'date_subscribed',
'pk'
)
return render(request, 'place/place_detail.html', {'place': place, 'placeSubscribers': placeSubscribers})
Anda 最后更新了我的模板,以便从 placeSubscribers 中提取数据,在那里我可以访问 date_subscribed 字段
{% for placeSubscriber in placeSubscribers %}
<a href="#" class="list-group-item clearfix">
<span class="pull-left">
{{ forloop.counter }}.  
</span>
<span class="pull-left">
<strong>{{ placeSubscriber.user__full_name }}</strong>
<p>Email: <i>{{ placeSubscriber.user__email }}</i> | Date Subscribed: <i> {{ placeSubscriber.date_subscribed }} </i> </p>
</span>
<span class="pull-right">
<span class="btn btn-xs btn-primary" onclick="sendPushNotificationToUser('{{ placeSubscriber.user__ionic_id }}'); return false;">Send Message</span>
<span class="btn btn-xs btn-danger" onclick="deletePlaceUser({{ place.id }}, {{ placeSubscriber.pk }}); return false; ">
Unsubscribe
</span>
</button>
</span>
</a>
{% endfor %}
我有这些模型,
class Place(models.Model):
name = models.CharField(max_length=200)
user = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='places', default=1)
subscribers = models.ManyToManyField(AppUser, through='PlaceSubscriber')
def __str__(self): # __unicode__ on Python
return self.name
class PlaceSubscriber(models.Model):
place = models.ForeignKey(Place, on_delete=models.CASCADE)
user = models.ForeignKey(AppUser, on_delete=models.CASCADE)
date_subscribed = models.DateTimeField(editable=False, default=timezone.now)
class Meta:
unique_together = ('place', 'user')
我想访问模板中此 for 循环的 date_subscribed 字段
{% for o in place.subscribers.all %}
<a href="#" class="list-group-item clearfix">
<span class="pull-left">
{{ forloop.counter }}.  
</span>
<span class="pull-left">
<strong>{{ o.full_name }}</strong>
<p>Email: <i>{{ o.email }}</i> | Date Subscribed: <i> {{
o.place__placesubscriber__date_subscribed }} </i> </p>
</span>
<span class="pull-right">
<span class="btn btn-xs btn-primary"
onclick="sendPushNotificationToUser('{{ o.ionic_id }}'); return
false;">Send Message</span>
<span class="btn btn-xs btn-danger" onclick="deletePlaceUser({{ place.id
}}, {{ o.id }}); return false; ">
Unsubscribe
</span>
</button>
</span>
</a>
{% endfor %}
我可以像这样访问这个 foorloop 之外的 date_subscribed 字段:
{% for each in place.placesubscriber_set.all %}
{{ each.date_subscribed }}
{% endfor %}
但还没有弄清楚如何在另一个内部访问它。
更新
这是我的看法
class PlaceDetailView(DetailView):
model = Place
template_name = 'place/place_detail.html'
这是 url 模式
url(r'^place/(?P<pk>\d+)$', views.PlaceDetailView.as_view(), name='detail_place'),
我认为在你的 views.py 文件中你应该这样做:
from django.shortcuts import render
from .models import Place
def placeview(request):
place = Place()
return render(request, 'place.html', {'place':place})
我通常会尝试在我的模板中进行良好的查询来处理简单的数据结构,避免复杂的逻辑。
我会将视图替换为一个简单的视图,它可以进行您想要的查询(希望如此):
def place_detail(request):
data = PlaceSubscriber.objects.all().values(
'user__full_name',
'user__email',
'place__name',
'date_subscribed'
)
return render(request,'place.html',{'data':data})
这会生成一个包含查询键的字典列表。您现在可以通过以下方式显示此数据:
{% for el in data %}
{el.user__full_name}
{el.date_subscribed}
...
{% endfor %}
嗯,我不确定你是否想要,但你可以更改查询值以获得你想要的。
希望对您有所帮助。
我最终采用了@Tico 方法,我更新了我的视图,因此逻辑比以前少了,这是我的更新:
更新了我的 urls.py 文件,停止使用通用详细信息视图 Class
url(r'^place/(?P<place_id>\d+)$', views.place_detail, name='detail_place'),
将 views.py 文件的详细视图方法更新为 return 我需要的两个模型的数据:
def place_detail(request, place_id):
place = get_object_or_404(Place, pk=place_id)
placeSubscribers = PlaceSubscriber.objects.all().filter(place=place).values(
'user__full_name',
'user__email',
'user__ionic_id',
'place__name',
'date_subscribed',
'pk'
)
return render(request, 'place/place_detail.html', {'place': place, 'placeSubscribers': placeSubscribers})
Anda 最后更新了我的模板,以便从 placeSubscribers 中提取数据,在那里我可以访问 date_subscribed 字段
{% for placeSubscriber in placeSubscribers %}
<a href="#" class="list-group-item clearfix">
<span class="pull-left">
{{ forloop.counter }}.  
</span>
<span class="pull-left">
<strong>{{ placeSubscriber.user__full_name }}</strong>
<p>Email: <i>{{ placeSubscriber.user__email }}</i> | Date Subscribed: <i> {{ placeSubscriber.date_subscribed }} </i> </p>
</span>
<span class="pull-right">
<span class="btn btn-xs btn-primary" onclick="sendPushNotificationToUser('{{ placeSubscriber.user__ionic_id }}'); return false;">Send Message</span>
<span class="btn btn-xs btn-danger" onclick="deletePlaceUser({{ place.id }}, {{ placeSubscriber.pk }}); return false; ">
Unsubscribe
</span>
</button>
</span>
</a>
{% endfor %}