Ticket #13203: 13203_r12773.diff

File 13203_r12773.diff, 1.4 KB (added by ramusus, 15 years ago)

patch for solving issue

  • django/contrib/contenttypes/generic.py

     
    123123        return smart_unicode([instance._get_pk_val() for instance in qs])
    124124
    125125    def m2m_db_table(self):
    126         return self.rel.to._meta.db_table
     126        # check current model with all its parents for generic relation fields
     127        model = self.rel.to
     128        object_id_field = model._meta.get_field(self.object_id_field_name)
     129        content_type_field = model._meta.get_field(self.content_type_field_name)
    127130
     131        # get list with current model and all its parents
     132        models = [model]
     133        while True:
     134            if model._meta.parents:
     135                model = model._meta.parents.keys()[0]
     136                models += [model]
     137            else:
     138                break
     139
     140        for model in models:
     141            # check that both fields inside this model
     142            if len(set(model._meta.local_fields) & set([object_id_field,content_type_field])) == 2:
     143                return model._meta.db_table
     144
     145        raise Exception("Can't find model with '%s' and '%s' fields"
     146            % (self.object_id_field_name, self.content_type_field_name))
     147
    128148    def m2m_column_name(self):
    129149        return self.object_id_field_name
    130150
Back to Top