

def add_to_class(self, model_class, name):
    if isinstance(self.rel_model, Proxy):

        def callback(rel_model):
            self.rel_model = rel_model
            self.add_to_class(model_class, name)
        self.rel_model.attach_callback(callback)
        return
    elif isinstance(self.rel_model, DeferredRelation):
        self.rel_model.set_field(model_class, self, name)
        return
    self.name = name
    self.model_class = model_class
    self.db_column = (self.db_column or ('%s_id' % self.name))
    if (not self.verbose_name):
        self.verbose_name = re.sub('_+', ' ', name).title()
    model_class._meta.add_field(self)
    self.related_name = self._get_related_name()
    if (self.rel_model == 'self'):
        self.rel_model = self.model_class
    if (self.to_field is not None):
        if (not isinstance(self.to_field, Field)):
            self.to_field = getattr(self.rel_model, self.to_field)
    else:
        self.to_field = self.rel_model._meta.primary_key
    if model_class._meta.validate_backrefs:
        if (self.related_name in self.rel_model._meta.fields):
            error = 'Foreign key: %s.%s related name "%s" collision with model field of the same name.'
            raise AttributeError((error % (self.model_class._meta.name, self.name, self.related_name)))
        if (self.related_name in self.rel_model._meta.reverse_rel):
            error = 'Foreign key: %s.%s related name "%s" collision with foreign key using same related_name.'
            raise AttributeError((error % (self.model_class._meta.name, self.name, self.related_name)))
    setattr(model_class, name, self._get_descriptor())
    setattr(model_class, (name + '_id'), self._get_id_descriptor())
    setattr(self.rel_model, self.related_name, self._get_backref_descriptor())
    self._is_bound = True
    model_class._meta.rel[self.name] = self
    self.rel_model._meta.reverse_rel[self.related_name] = self

dataset/ETHPy150Open coleifer/peewee/peewee.py ForeignKeyField.add_to_class/original