diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py index bc225f584f..22bd4817d6 100644 --- a/django/core/meta/__init__.py +++ b/django/core/meta/__init__.py @@ -947,8 +947,8 @@ def method_add_related(rel_obj, rel_mod, rel_field, self, *args, **kwargs): # Handles related many-to-many object retrieval. # Examples: Album.get_song(), Album.get_song_list(), Album.get_song_count() -def method_get_related_many_to_many(method_name, rel_mod, rel_field, self, **kwargs): - kwargs['%s__id__exact' % rel_field.name] = self.id +def method_get_related_many_to_many(method_name, opts, rel_mod, rel_field, self, **kwargs): + kwargs['%s__%s__exact' % (rel_field.name, opts.pk.name)] = getattr(self, opts.pk.column) return getattr(rel_mod, method_name)(**kwargs) # Handles setting many-to-many related objects. diff --git a/django/models/__init__.py b/django/models/__init__.py index 32350200a0..2937223361 100644 --- a/django/models/__init__.py +++ b/django/models/__init__.py @@ -62,9 +62,9 @@ for mod in modules: for rel_opts, rel_field in klass._meta.get_all_related_many_to_many_objects(): rel_mod = rel_opts.get_model_module() rel_obj_name = klass._meta.get_rel_object_method_name(rel_opts, rel_field) - setattr(klass, 'get_%s' % rel_obj_name, curry(meta.method_get_related_many_to_many, 'get_object', rel_mod, rel_field)) - setattr(klass, 'get_%s_count' % rel_obj_name, curry(meta.method_get_related_many_to_many, 'get_count', rel_mod, rel_field)) - setattr(klass, 'get_%s_list' % rel_obj_name, curry(meta.method_get_related_many_to_many, 'get_list', rel_mod, rel_field)) + setattr(klass, 'get_%s' % rel_obj_name, curry(meta.method_get_related_many_to_many, 'get_object', klass._meta, rel_mod, rel_field)) + setattr(klass, 'get_%s_count' % rel_obj_name, curry(meta.method_get_related_many_to_many, 'get_count', klass._meta, rel_mod, rel_field)) + setattr(klass, 'get_%s_list' % rel_obj_name, curry(meta.method_get_related_many_to_many, 'get_list', klass._meta, rel_mod, rel_field)) if rel_opts.app_label == klass._meta.app_label: func = curry(meta.method_set_related_many_to_many, rel_opts, rel_field) func.alters_data = True