Django, filter by multiple values -
so have these models:
bands(models.model): mgmt = models.foreignkey(user) name = models.charfield(max_length=200) contracts(models.model): band = models.foreignkey(bands) start_date= models.datefield() bookedgig(models.model): = models.foreignkey(bands) under= models.foreignkey(contracts) date = models.datefield()
how construct in views.py file capture bookedgigs user? goal display through template, various gigs under title of contacts/bands.
in views.py have
def home(request): user = request.user bands = bands.objects.filter(mgmt=user).order_by('name') #this give me bands belonging user contracts = contracts.filter(band=bands) #but here bands not 1 value queryset. #if try contracts = bands.booked_gig_set.all() 'queryset' object has no attribute 'booked_gig_set'
templates: know wrong how i'd display lists.
{% b in bands %} band:{{b.name}} {% c in contracts %} contract start:{{c.start_date}} {% g in gigs %} {{g.dates}} {% endfor %} {% endfor %} {% endfor %}
thanks
contracts.objects.filter(band__in=bands)
you might want add prefetch_related
statement there prefetch gigs though, otherwise template loop hit db once per contract.
contracts = contracts.objects.filter(band__in=bands).prefetch_related()
Comments
Post a Comment