On Mar 18, 4:28 pm, "het.oosten" <[email protected]> wrote:
> I have this model:
> class House(models.Model):
> Huisnumber = models.CharField(max_length=3)
> Reservation = models.ForeignKey('Data', blank=True)
> def __unicode__(self):
> return self.Huisnumber
>
> class Data(models.Model):
> Name = models.CharField(max_length=30)
> Arrival = models.DateField()
> Departure = models.DateField()
> def __unicode__(self):
> return '%s %s %s' % (self.Naam, self.Arrival,
> self.Departure)
>
> And this query:
> range = House.objects.exclude(
>
> Reservation__Arrival__range=(Check_arrival, Check_departure)
> ).exclude(
>
> Reservation__Departure__range=(Check_arrival, Check_departure)
> )
>
> This should get a list of houses which are available. This works fine
> when only one date (reservation) is entered. When a house has two
> reservations, only the last entry is evaluated. How should I handle
> multiple entries?
You have your relationship the wrong way round - as is, a House can
only be linked to one single Reservation. It's "Data" (which you
should really rename "Reservation") that should have a foreign key on
House, ie:
class House(models.Model):
huisnumber = models.CharField(max_length=3)
def __unicode__(self):
return u"%s" % self.huisnumber
class Reservation(models.Model):
house = models.ForeignKey(House, related_name="reservations")
name = models.CharField(max_length=30)
arrival = models.DateField()
departure = models.DateField()
def __unicode__(self):
return u"%(name)s %(arrival)s %(departure)s" %
self.__dict__
HTH
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.