Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing date field from foreign key

I am trying to use a generic date view on a model which has a foreign key to another model where the date is stored. My view class looks like this.

class MileageYearView(YearArchiveView):
    queryset = Miles.objects.all()
    date_field = 'ride__date'
    make_object_list = True

here is what my models look like

class Ride(models.Model):
    profile = models.ForeignKey(RideProfile)
    leader = models.ForeignKey(Rider)
    date = models.DateTimeField('date of ride')
    approved = models.BooleanField(default=False)

class Miles(models.Model):
    rider = models.ForeignKey(Rider)
    ride = models.ForeignKey(Ride)
    actual_miles = models.FloatField('actual miles')

And the error I'm encounterint is Miles has no field named 'ride__date'

I'm new to this, and in terms I can understand I am trying to join the two models so I can view all the mile entries for one year.

like image 908
dzanot Avatar asked Mar 04 '26 08:03

dzanot


1 Answers

Only way to achieve this that I've found is to create a @property in class Miles that returns the ride date, as follow:

@property
def ride_date( self ):
    return self.ride.date

and use 'ride_date' as the value for date_field in class MileageYearView

Hope it helps!

like image 111
Jotakun Avatar answered Mar 06 '26 02:03

Jotakun