2016-05-12 2 views
1

認証されたユーザーが行ったすべての予約をテンプレートに表示できる必要があります。私はモデルマネージャを使って、必要なプロパティを返す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>

私は間違って何をしているのか?

+0

インポートpdbを試しましたか? pdb.set_trace()を呼び出すと、予約の内容を確認できます。 – jsanchezs

答えて

1

リクエスト(つまり現在のユーザー)のプロパティを選択しようとしていますが、マネージャはリクエストとは独立して存在します。

組み込みのクラスビューを使用しようとしましたか?そこでは、同等のget_queryset方法は、利用可能なself.requestとを持つクラスのインスタンスであるため、少し

https://docs.djangoproject.com/en/1.9/topics/class-based-views/generic-display/で)Djangoのドキュメントの例をRewording self.request.user:

class ReservationList(ListView): 

    template_name = 'reservation_list.html' 

    def get_queryset(self): 
     return Reservation.objects.filter(customer=self.request.user) 

私はちょうどました例をコピーして貼り付けてハッキングしましたが、うまくいけば、進歩を遂げるためには十分に近いです。

関連する問題