django custom manager with filter parameter -
i add custom manager can called template, not affect entire model (e.g. admin views) , listens parameter set in request (user_profile).
the following have far:
models.py:
class currentqueryset(models.query.queryset): def current(self): return self.filter(id=1) ## simplified filter test works.. class currentmanager(models.manager): use_for_related_fields = true def get_query_set(self): return currentqueryset(self.model) def current(self, *args, **kwargs): return self.get_query_set().current(*args, **kwargs)
for model b defined:
objects = currentmanager()
the template calls:
{% b in a.b_set.current %}
but try pass parameter filter (in case date stored on user-profile) method not return results.
e.g.:
models.py
class currentqueryset(models.query.queryset): def current(self,my_date): return self.filter(valid_from__lte=my_date)
showa.html
{% b in a.b_set.current(request.user.get_profile.my_date) %}
instead of passing parameter template, have tried set in view.py
@login_required def showa(request,a_id): my_date = request.user.get_profile().my_date = a.objects.get(id=a_id) t = loader.get_template('myapp/showa.html') c = requestcontext(request,{'a':a,'my_date':my_date,}) return httpresponse(t.render(c))
which part missing (or misunderstanding) here?
thanks
r
edit
here models. mentioned, in example it's simple 1:n relationship, can m:n in other cases.
class a(models.model): #objects = currentmanager() = models.charfield(max_length=200) description = models.textfield(null=true,blank=true) valid_from = models.datefield('valid from') valid_to = models.datefield('valid to',null=true,blank=true) def __unicode__(self): return self.a class b(models.model): #objects = models.manager() objects = currentmanager() = models.foreignkey(a) b = models.charfield(max_length=200) screenshot = models.manytomanyfield("screenshot",through="screenshottob") description = models.textfield(null=true,blank=true) valid_from = models.datefield('valid from') valid_to = models.datefield('valid to',null=true,blank=true) def __unicode__(self): return self.b
edit-2
the accepted answer works @ least 1 relationship.
in case of more nested data model, method seems not deliver expected results:
models.py
class c(models.model): objects = currentmanager() b = models.foreignkey(a) c = models.charfield(max_length=200) description = models.textfield(null=true,blank=true) valid_from = models.datefield('valid from') valid_to = models.datefield('valid to',null=true,blank=true) def __unicode__(self): return self.c
views.py
@login_required def showa(request,a_id): = a.objects.get(id=a_id) my_date = request.user.get_profile().my_date b_objects = a.b_set.current(my_date) c_objects = b_objects.c_set.current(my_date) t = loader.get_template('controltool2/showa.html') c = requestcontext(request,{'a':a,'b_objects':b_objects,'c_objects':c_objects,}) return httpresponse(t.render(c))
this returns error: 'queryset' object has no attribute 'c_set'
i'd simplify it:
class currentmanager(models.manager): def current(self, my_date): return super(currentmanager, self).get_query_set().filter(valid_from__lte=my_date)
and use this:
a = a.objects.get(id=a_id) my_date = request.user.get_profile().my_date b_objects = a.b_set.objects.current(my_date)
and pass template filtered objects accessing them using this:
{% b in b_objects %}
hope helps!
edit (by requestor):
i had adjust follows working:
a = a.objects.get(id=a_id) my_date = request.user.get_profile().my_date b_objects = a.b_set.current(my_date)
this threw error: "'relatedmanager' object has no attribute 'objects'"
a.b_set.objects.current(my_date)
Comments
Post a Comment