Задать вопрос

Тел: +7 965 3737 888

447

Просмотров

5

Ответов

Get child model

With the child_object function defined in the parent model, you can get it's child object.

Вопрос полезен? Да0/Нет0
file_3634.py(447.0б)
None

Ответы (5):

Ответedwardgeorge:11.05.2008
Ответ полезен? Да0/Нет0

then in place of the child_object method in the example above you could use the following descriptor on Parent to get the child object:

child_object = property(lambda self: getattr(self, self.object_name.lower()))

Ответedwardgeorge:11.05.2008
Ответ полезен? Да0/Нет0

how about adding a field to Parent called object_name and override the save method on Parent to assign

self.object_name = self._meta.object_name

I've just tried this in some of my code and it works for me

Ответandreas:11.05.2008
Ответ полезен? Да0/Нет0

I need aswell but i dont think there is any at the moment. This has been discussed on the irc channel.

Ответhangya:10.05.2008
Ответ полезен? Да0/Нет0

Well, this may be true; please tell me if you know a better solution or a workaround for this task, which I need often...

Ответandreas:10.05.2008
Ответ полезен? Да0/Нет0

This will is not a good solution since it will lead to lots of unnecessary load on the database (each self. will be a select query, possible with joins)