On Feb 16, 1:56 pm, Derek <gamesb...@gmail.com> wrote:
> (not a movie trivia problem!)
>
> The question I need to resolve here is "does Passenger 57 qualify for a
> discount"?
>
> Given the following models:
>
> class Alliance(models.Model):
>     name = models.CharField(max_length=100)
>     #e.g. Star, Western, Pacific, European
>     discount = models.FloatField()
>
> class Airline(models.Model):
>     name = models.CharField(max_length=100)
>     #e.g. AIA, Northwest, Cathay, KLM
>     membership = models.ManyToManyField(Alliance)
>
> class Flight(models.Model):
>     name = models.CharField(max_length=100)
>     operator = models.ForeignKey(Airline)
>
> class Passenger(models.Model):
>     name = models.CharField(max_length=100)
>     flight = models.ForeignKey(Flight)
>     memberships = models.ManyToManyField(Alliance)
>
> So, if Passenger 57 books a flight with, say, AIA, which is a member of the
> Star and Western alliances, how can I tell if this qualifies for a
> discount?  Assume, for purpose of this query, that the logged in user_id is
> the same as the passenger_id (i.e. 57).

How about creating this as a method of the passenger model:

def calculateDiscount(self):
    max_discount = 0
    # discount_with = None
    flight_allies = self.flight.operator.membership.all()
    # you may want to make the above "memberships" (with an s) to be
consistent or it will come back to bite you in the bum!
    users_allies = self.memberships.all()
    for fa in flight_allies:
        if fa in users_allies:
            if fa.discount > max_discount:
                max_discount = fa.discount
                # discount_with = fa
    return max_discount

I put the 'discount_with' variable in so that if you wanted to know
which membership was used for a discount you can (just need to figure
out the best way to return it - I tend to use objects or dicts). I
haven't in any way tested it, so there may be silly errors, but you
get the idea. There are probably also better (more efficient) ways,
but this is what would occur to me!

To use this form a view, you would do something like:

passenger = Passenger.objects.get(pk=57)
discount = passenger.calculateDiscount()

Presumably you know how to get the id of the logged in user (you would
just put that in instead of 57).

Em

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to