django - How can I change the returned value of a field in a QuerySet based on some passed parameter? -



django - How can I change the returned value of a field in a QuerySet based on some passed parameter? -

i want able homecoming different values queryset (not changing database) based on parameter. i'd able write model enforced everywhere.

if user type of user, want queryset field blank (or "hidden" or similar).

here's simplified model:

class somedetails(models.model): size = models.floatfield() this_is_okay_to_show = models.textfield() not_always_ok = models.textfield()

simplified queryset:

qssomedetails = somedetails.objects.all()

i want not_always_ok either homecoming text value stored in database or homecoming empty string (or 'hidden' or similar).

template filter work, needs in model.

i'm not sure how pass parameter through create work.

i sense reply right in front end of me, i'm not seeing it.

define manager class somedetails model:

class somedetailsmanager(models.manager): def __getattr__(self, attr, *args): try: homecoming getattr(self.__class__, attr, *args) except attributeerror: homecoming getattr(self.get_query_set(), attr, *args) def get_query_set(self): homecoming self.model.queryset(self.model)

and alter somedetails this:

from django.db.models.query import queryset class somedetails(models.model): size = models.floatfield() this_is_okay_to_show = models.textfield() not_always_ok = models.textfield() objects = somedetailsmanager() class queryset(queryset): def get_user(self, user, *args, **kwargs): _objects = self.filter(*args, **kwargs) obj in _objects: if user.username = 'foo': #change status obj.not_always_ok = '' homecoming _objects

now can utilize

qssomedetails = somedetails.objects.filter(pk=1).get_new_objects(request.user)

note:

first done filter , utilize get_new_objects(request.user) lastly part.

django

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -