2013-10-06 183 views
0

我一直在關注Django 1.4的通用視圖手冊,但可以通過發佈者列表的示例工作。我的網站略有不同,因爲我試圖按登記財產的人的姓名(或身份證)列出財產的預訂。人們會預訂不止一次,所以我希望能夠看到他們的預訂是什麼。django基於類的視圖關鍵詞

我給這家views.url是:

class GuestBookingListView(DetailView): 

context_object_name = 'guest_booking' 
template_name = 'guest_booking.html' 

def get_queryset(self): 
    self.guest = get_object_or_404(Guest) 
    return Booking.objects.filter(guest = self.guest) 

def get_context_data(self, **kwargs): 
    context = super(GuestBookingListView, self).get_context_data(**kwargs) 
    context['guest'] = self.guest 
    return context 

我的模式是:

class Guest(models.Model): 
first_name = models.CharField(max_length=30) 
last_name = models.CharField(max_length=50) 
spouse_first = models.CharField(max_length=30, blank=True) 
spouse_last = models.CharField(max_length=50, blank=True) 
num_child = models.IntegerField(verbose_name='Number of children') 
address = models.TextField(max_length=50, blank=True) 
city = models.CharField(max_length=60, blank=True, verbose_name='Town/City') 
state_province = models.CharField(max_length=30, blank=True, verbose_name='County') 
post_code = models.CharField(max_length=8, blank=True) 
country = models.CharField(max_length=50, blank=True) 
email = models.EmailField(blank=True) 
landline = models.CharField(max_length=25, blank=True) 
mobile = models.CharField(max_length=25, blank=True) 

def __unicode__(self): 
    return u'%s %s' % (self.first_name, self.last_name) 

class Booking(models.Model): 
    guest = models.ForeignKey(Guest) 
    ack_date = models.DateField(verbose_name='Date acknowledged') 
    start_date = models.DateField(verbose_name='Start date') 
    end_date = models.DateField(verbose_name='End date') 
    dep_recd = models.DateField(null=True, blank=True, verbose_name='Deposit received') 
    bal_recd = models.DateField(null=True, blank=True, verbose_name='Balance received') 
    keys_sent = models.DateField(null=True, blank=True, verbose_name='Date keys sent') 
    sec_retn = models.DateField(null=True, blank=True, verbose_name='Security deposit returned') 
    rtm_sent = models.IntegerField('Status', blank=True) 
    notes = models.TextField(blank=True, verbose_name='Notes') 

和我的urls.py是:

url(r'^guests/(?P<pk>\d+)/$', GuestBookingListView.as_view (
    #context_object_name = 'booking_list', 
    )), 

到目前爲止,我所看到的這是相同的(與不同的字段名稱)的例子,但我得到的結果是:

get()返回多個Guest - 它返回26!查找參數是{}

'get'正在檢索數據庫中的所有Guests,而不是我選擇的那個。

我花了數小時搜索和試驗這個,但無濟於事。如果我把'客人= 11'它的工作,所以有問題的PK。

謝謝!

回答

0

你還沒有給出任何標準來獲得客人。實際上,你剛剛說過,「給我客人」,Django給你所有26個。如果你想過濾pk克瓦格,你應該這樣說:

self.guest = get_object_or_404(Guest, pk=self.kwargs['pk']) 
+0

謝謝丹尼爾。我知道它必須是這樣的,但無法找到正確的形式。 – sidestrand