W
W
wiseCoders2015-08-29 10:18:28
Django
wiseCoders, 2015-08-29 10:18:28

How to make a generic function in model.py?

Good day to all. There is a function in model.py that pulls out the picture of the record from the parent if the child does not have one. In addition to the picture, there are other fields that can be pulled out according to the same principle, for example, a city. Whether prompt it is possible to universalize this function and to request through a template with parameters of a necessary field.

def get_image(self):
        current = self
        while not current.image and current.parent is not None:
            current = current.parent
        if current.image:
            return current.image.url
        return None

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey K, 2015-08-29
@mututunus

https://docs.python.org/2/reference/datamodel.html...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question