1👍
Check select_related if you want include referenced objects
EventCounty.objects.select_related('event').filter(....)
👤Krab
0👍
As you defined a event can be in multiple counties. So event object has set of County
you can access them as
event_obj.county_set.all()
county_set()
is RelatedManager
so you can do .filter()
, .get()
etc on it.
- [Answer]-Running submit() twice to submit to different forms at once
- [Answer]-Login_required – login success not redirecting to "next"
0👍
objs = EventCounty.objects.filter(event__in=listevents)
countyList = []
for obj in objs:
countyList.append(obj.county)
- [Answer]-Image in Django Template doesn't appear
- [Answer]-Django templates are not showing values of annotations
- [Answer]-Specify a form field as not required at instance creation
- [Answer]-Partial Load from Ember Model
0👍
As the EventCount model holds a ForeignKey to Event model it is rather simple – ForeignKey enables “backward” relation (see Django documentation). In your case this could look like:
events = Event.objects.all().order_by(<put your date field here>)[:5]
To get a list (or QuerySet) off all EventCounty objects for e.g. first Event object just use:
events_county = events[0].eventcounty_set.all()
As suggested by Rohan the eventcounty_set is a RelatedManager which means you can work with it like with a QuerySet (filter, exclude, order, slice etc.)
Source:stackexchange.com