I have a Manager fucntion that prefetches cover photo of the model.The cover_photo (Foreign key to a image model) model has a field
is_verified . Is there a way to make the cover_photo object
None if it has
is_verified=False from the manager? So that from normal user point view I can hide unverified image model contents and from admin side no restrictions.
(this manager function is for listing deities for normal user.)
The function inside my manager:
temple_objs = self.prefetch_related(
# for temple_obj in temple_objs: # Unsuccessful attempt
# for deity_obj in temple_obj.deities_list:
# # pass
# if not deity_obj.cover_photo.is_verified:
# deity_obj.cover_photo = None
The commented sections was my try, and it failed. What should I do to achieve this.
The easiest way to do this would be to assign a property function to the model, not in the manager. This function can perform the test and return either the photo field or None. Your templates and other user-facing code can then retrieve the field through the function rather than accessing the field directly. This also allows the admin to work without being affected by this.
I will go with that, and why wouldn’t I think of property before. But is there way through Manager for this? I am just curious cause, I wasted 2 days on trying that on manager.
but however I was able to familiarize new things like When - Then and Case conditionals in my way.)
It’s a difference of function & focus between the two. I look at Managers as working with querysets - a set of rows satisfying some condition. I look at Model methods as doing work for individual instances. So I wouldn’t try doing this through a Manager - I don’t see the value of it.
On the other hand, you do have an option with using annotations and the When, Then, and Case constructs to inject a new field in each row. You could annotate each row with the result of a test on the
is_verified field where the new field is assigned either None or the cover_photo object.