0

I can use some help on a query using 3 tables. Here is what my models contains.

class VolunteerRecord(models.Model):
    eventname = models.CharField(help_text=_('Name of the event'),max_length=256, blank=False, default='')
    category = models.CharField(max_length=256, blank=False, choices=CATEGORY_CHOICES)
    hours = models.FloatField(blank=False)
    date=models.DateField(help_text=_('Enter the date of the event'),validators=MaxValueValidator(limit_value=date.today)])
    mileage = models.FloatField(blank=True, null=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    streetaddress1 = models.CharField(blank=True, max_length=256)
    streetaddress2 = models.CharField(blank=True, max_length=256)
    city = models.CharField(blank=True, max_length=30)
    state = models.CharField(max_length=256, blank=False, choices=US_STATES)
    zipcode = models.CharField(blank=True, max_length=15)
    county = models.CharField(max_length=256, blank=False, choices=STATE_COUNTIES)

I would like to filter all VolunteerRecords where the owner's county = request.user's county

So far I have... (not sure what I am suppose to put where my ??? are)

def history(request):
   current_user_county = request.user.profile.county
   records = VolunteerRecord.objects.filter(????=current_user_county)
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1msmith1
  • 3
  • 2
  • Does this answer your question? [Django filter foreignkey field](https://stackoverflow.com/questions/24547532/django-filter-foreignkey-field) – sytech Feb 03 '21 at 21:18
  • `VolunteerRecord.objects.filter(owner__county=current_user_county)` – sytech Feb 03 '21 at 21:19

1 Answers1

0

I was able to figure it our with your help.

 records = VolunteerRecord.objects.filter(owner__profile__county=request.user.profile.county)
Tomek
  • 323
  • 1
  • 4
  • 19
1msmith1
  • 3
  • 2