Ticket #13203: 13203_r12773_2.diff

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

Update patch with respect to russellm comment (ver 2)

  • django/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        main_model = self.rel.to
     128        object_id_field = main_model._meta.get_field(self.object_id_field_name)
     129        content_type_field = main_model._meta.get_field(self.content_type_field_name)
    127130
     131        object_id_model = content_type_model = None
     132
     133        for field, model in main_model._meta.get_fields_with_model():
     134            model = model or main_model
     135            if field == object_id_field:
     136                object_id_model = model
     137            elif field == content_type_field:
     138                content_type_model = model
     139
     140        if object_id_model and object_id_model == content_type_model:
     141            return object_id_model._meta.db_table
     142        else:
     143            raise Exception("Fields '%s' and '%s' are located in different models"
     144            % (self.object_id_field_name, self.content_type_field_name))
     145
    128146    def m2m_column_name(self):
    129147        return self.object_id_field_name
    130148
Back to Top