Exemple #1
0
def create_intermediary_model(m2m, model, name):
    is_polymorph = m2m.to_field is None
    
    if is_polymorph:
        source, target = m2m.rel.to, model
    else:
        source, target = model, m2m.rel.to
        
    if source._meta.abstract:
        return None

    if not is_polymorph:
        table = m2m.to_field.rel.through._meta.db_table
    elif m2m.explicit_db_table:
        table = m2m.explicit_db_table
    else:
        table = "%s_%s_%s_rel" % (model._meta.app_label, model.__name__.lower(), name)
    
    on_delete = models.CASCADE if is_polymorph else models.DO_NOTHING,
    
    attrs = dict(
        obj = models.ForeignKey(source, related_name=no_related_name(), on_delete=on_delete),
        rel_obj = models.ForeignKey(target, related_name=no_related_name(), on_delete=on_delete),
        Meta = type('Meta', (object,), dict(
            managed=is_polymorph, 
            db_table=table,
            auto_created=model,
            unique_together=('obj', 'rel_obj'),
        )),
        __module__ = target.__module__,
    )
    
    through = type('%s%s' % (model.__name__, camel_case(name, True)), (models.Model,), attrs)
    return through
Exemple #2
0
    def __init__(self, *args, **kwargs):
        self.related_manager_class = kwargs.pop('related_manager_class', None)
        self.related_manager_class_decorator = kwargs.pop('related_manager_class_decorator', None)

        if 'related_name' in kwargs and not kwargs['related_name']:
            kwargs['related_name'] = no_related_name()
        super(ForeignKey, self).__init__(*args, **kwargs)
Exemple #3
0
 def __init__(self, *args, **kwargs):
     if 'related_name' in kwargs and not kwargs['related_name']:
         kwargs['related_name'] = no_related_name()
     super(OneToOneField, self).__init__(*args, **kwargs)