Reference class in class creation

Gregor Horvath gh at
Tue Nov 21 14:48:41 CET 2006

Duncan Booth schrieb:

> You've told us that what you are trying to do doesn't work, but you haven't 
> said why you want to do it. What is the problem you are really trying to 
> solve?

I need this for some complicated turbogears controller classes for a
generic framework.

The point is that I have a formwidget which has to know which controller
it belongs to. (Because the controller knows the path, the
sqlobjectclass etc.). The widget has to be created at class creation
time of the controller, because there is a metaclass which ensures
validation of the formwidget. (Necessary because the controller is

Basically and stripped down this looks like this:

class ControllerMeta(type):
    def __new__(meta, class_name, bases, new_attrs):
        new_attrs["save"] = validate(new_attrs['widget_edit'])
        return type.__new__(meta, class_name, bases, new_attrs)

class MotourForm(TableForm):
    template = "motour.templates.motourform"
    params = ["delete_attrs","cancel_attrs"]

    member_widgets = ["delete","cancel"]
    cancel = ResetButton(name="cancel")
    delete_attrs = {}

    def __init__(self, controller, *args, **kwargs):
        super(MotourForm, self).__init__(action = "%s/save" %
controller.get_path, *args, **kwargs)
        self.controller = controller
        self.delete = LinkButtonAsk(name="delete", caption =
_(u"Löschen"), question = _(u"Wirklich loeschen"),
        link = "%s/delete" % controller.get_path())

        self.cancel_attrs = dict(onClick="self.location.href='%s/back';"
                                 % controller.get_path())

class TourEdit(MotourForm):
    template = "motour.templates.touredit"
    params = ["tourposvalue"]

    member_widgets = ["tourpos"]
    tourpos = DataGridAED()

class Tour(MotourController):
  __metaclass__ = ControllerMeta
  sqlobjectclass = model.Tour

  #Here is the problem
  widget_edit = TourEdit(controller=Tour, name = "tourheader",
                         fields = [Label(name="id", label=_("Tour-ID")),

I hope it's not to confusing.
Thank's for your help.


More information about the Python-list mailing list