認証されたユーザーが行ったすべての予約をテンプレートに表示できる必要があります。私はモデルマネージャを使って、必要なプロパティを返すget_querysetメソッドをオーバーライドしています。モデルマネージャを使用して認証されたユーザーが行ったエントリを表示する
次に、テンプレートに渡す前にビューのクエリセットとして使用します。私はdocumentation on managersに従ったときに私が何をしていないのか分かりません。
models.py
class ReservationManager(models.Manager):
use_for_related_fields = True
def get_queryset(self):
return super(ReservationManager, self).get_queryset().filter(customer_name=User)
class Reservation(models.Model):
"""
this class will contain all information that concerns a car reservation
"""
customer_name = models.ForeignKey(User)
vehicle = models.ForeignKey(Car)
pickup_location = models.ForeignKey(Location)
drop_location = models.ForeignKey(Location, related_name='drop_location')
pickup_time = models.DateTimeField(blank=False)
drop_time = models.DateTimeField(blank=False)
reserved_on = models.DateTimeField(auto_now_add=True)
edited_on = models.DateTimeField(auto_now=True)
completed = models.BooleanField(default=False)
reservations = ReservationManager()
views.py
class ReservationsList(ListView):
model = Reservation
queryset = Reservation.reservations.all()
template_name = 'reservation_list.html'
context_object_name = 'reservations'
`
テンプレート
テンプレートは、認証されたユーザーによって行われたすべての予約が表示されます。
<tbody>
{% if user.is_authenticated %}
{% for reservation in reservations %}
<tr class="row1"><td class="action-checkbox"><input class="action-select" name="_selected_action" type="checkbox" value="2" /></td>
<th class="field-code white-text grey center">{{reservation.code}}</th>
<td class="field-customer_name nowrap">{{reservation.customer_name}}</td>
<td class="field-vehicle nowrap">{{reservation.vehicle}}</td>
<td class="field-pickup_location nowrap white-text grey center">{{reservation.pickup_location}}</td>
<td class="field-drop_location nowrap">{{reservation.drop_location}}</td>
<td class="field-pickup_time nowrap white-text grey center">{{reservation.pickup_time}}</td>
<td class="field-drop_time nowrap ">{{reservation.drop_time}}</td>
<td class="field-reserved_on white-text grey center nowrap">{{reservation.reserved_on}}</td>
</tr>
{% endfor %}
{% else %}
nothing
{% endif %}
</tbody>
私は間違って何をしているのか?
インポートpdbを試しましたか? pdb.set_trace()を呼び出すと、予約の内容を確認できます。 – jsanchezs