Question

I've been following the manual for generic views for Django 1.4, but can get the 'list books by publisher' example to work. My site is slightly different in that I'm trying to list bookings of a property by the name (or id) of the person who books the property. People will book more than once, so I want to be able to see what their bookings were.

My views.url for this is:

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

My model is:

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')

and my urls.py is:

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

So far as I can see this identical (with different field names) to the example, but the result I get is:

get() returned more than one Guest -- it returned 26! Lookup parameters were {}

The 'get' is retrieving all of the Guests in the database, not the one which I've selected.

I've spent hours of searching and experimenting on this, but to no avail. If I put 'guest = 11' it works, so there's something wrong with the pk.

Thank you!

Was it helpful?

Solution

You haven't given any sort of criteria to get the guest. You've just said, in effect, "give me guest", and Django has given you all 26 of them. If you want to filter by the pk kwarg, you should say so:

self.guest =  get_object_or_404(Guest, pk=self.kwargs['pk'])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top