user43944
user43944

Reputation: 159

Reverse Foreign Key Lookup

#models.py
class Orders(models.Model):
    orderid = models.IntegerField(db_column='orderID', primary_key=True) 
    createdate = models.DateField(db_column='createDate', blank=True, null=True) 
    pickupdate = models.DateField(db_column='pickupDate', blank=True, null=True)  
    returndate = models.DateField(db_column='returnDate', blank=True, null=True)  
    pickupstore = models.ForeignKey(Branch, models.DO_NOTHING, db_column='pickupStore', blank=True, null=True,related_name = 'pickupstore')  
    returnstore = models.ForeignKey(Branch, models.DO_NOTHING, db_column='returnStore', blank=True, null=True,related_name = 'returnstore') 
    rentedvehicle = models.ForeignKey('Vehicles', models.DO_NOTHING, db_column='rentedVehicle', blank=True, null=True)  
    customer = models.ForeignKey(Customer, models.DO_NOTHING, db_column='customer', blank=True, null=True)


class Vehicles(models.Model):
    vehicleid = models.IntegerField(db_column='vehicleID', primary_key=True)  
    make = models.CharField(max_length=45, blank=True, null=True)
    model = models.CharField(max_length=45, blank=True, null=True)
    series = models.CharField(max_length=45, blank=True, null=True)


#views.py
def topcar(request):
    topCar = Vehicles.objects.filter(orders__pickupstore__state = request.POST['state']).annotate(num_car =Count('orders')).order_by('-num_car')[:20]
    return render(request, 'web/topcar.html',{'topCar':topCar, 'state':request.POST['state'])




#topcar.html
{% for car in topCar %}
   <tr>
      <td>{{car.make}} {{car.model}} {{car.series}}  {{car.year}}</td>
      <td>{{car.seatcapacity}}</td>
      <td>{{the latest car return store}}</td>
   </tr>
{% endfor %}

In views.py, topCar variable is the list of top 20 vehicles (in Vehicles model) that have high orders in Orders model.The field rentedvehicle in Orders model is foreign key refers to Vehicles model.

I want to get the latest returnstore (a field in Orders model) for each car in topCar variable. I want it to be displayed in {{the latest car return store}} in topCar.html

How should I do that?

I find out that I can do this :

Orders.objects.filter(rentedvehicle = car.vehicleid).latest('returndate').returnstore

to get the latest returnstore for a vehicleid. However, I don't know how to use that for getting the result that I want.

Upvotes: 2

Views: 138

Answers (1)

Will Keeling
Will Keeling

Reputation: 22994

You can only use simple lookup logic in Django templates, so you wouldn't be able to do the latest('returndate') filtering there.

Instead, you could add a method to your Vehicle model to get the latest order:

class Vehicles(models.Model):
    vehicleid = models.IntegerField(db_column='vehicleID', primary_key=True)  
    make = models.CharField(max_length=45, blank=True, null=True)
    model = models.CharField(max_length=45, blank=True, null=True)
    series = models.CharField(max_length=45, blank=True, null=True)

    def latest_order(self):
        return self.orders_set.latest('returndate')

Then you should be able to modify your template to:

{% for car in topCar %}
   <tr>
      <td>{{car.make}} {{car.model}} {{car.series}}  {{car.year}}</td>
      <td>{{car.seatcapacity}}</td>
      <td>{{car.latest_order.returnstore}}</td>
   </tr>
{% endfor %}

Upvotes: 1

Related Questions