I am struggling to get my search function to pull in results from multiple models into a single table.
I have a Person Model and a Dates Model:
class Person(models.Model):
personid = models.AutoField()
name = models.CharField()
etc....
class Dates(models.Model):
datesid = models.AutoField()
personid = models.ForeignKey(Person, models.DO_NOTHING)
date_type = models.CharField()
date = models.CharField()
etc....
There will be multiple Dates per personid (many to one) what I am trying to do is return the newest date from Dates when a search is performed on the Person Model - View below:
def search(request):
if request.method == "POST":
searched = request.POST.get('searched')
stripsearched = searched.strip()
results = Person.objects.filter(
Q(searchterm1__icontains=stripsearched) |
Q(searchterm12__icontains=stripsearched) |
Q(searchterm3__contains=stripsearched)
).values('personid', 'name', 'address_1', 'address_2', 'address_3', 'code', 'number')
How would I add in the Top 1 newest related Dates.date into this for each Person found in the "results" field ?
View:
{% for Person in results %}
<tr>
<td>{{ Person.name }}</a></td>
<td >{{ Person.address_1 }}</td>
<td >{{ Person.code }}</td>
</tr>
{% endfor %}