Opened 14 years ago
Closed 14 years ago
#14327 closed (duplicate)
Custom Manager not honored in related queries
Reported by: | anonymous | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | 1.2 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Dear awesome Django team,
I believe the below is a bug based on the documentation: http://docs.djangoproject.com/en/dev/topics/db/managers/#controlling-automatic-manager-types.
In a gist if I have a custom manager for FOO that overwrites get_or_create() this will work:
FOO.objects.get_or_create(...)
but this will not:
BAR.foo_set.get_or_create(...) - this calls the default manager. It is worth pointing out that if my custom manager contains get_or_create2() than BAR.foo_set.get_or_create2(...) works.
class LogingManager(models.Manager): """ @author Daniel Sokolowski www.danols.com This manager extends the Default Manager to provide auto loging of manager's methods that result in database changes - object creations, deletions, changes. Transactions are logged with the django's built in LogEntry model which is part of the Auth module. Default manager methods are extended to accept an additional parameter ``log_under`` which accepts a User instance to log the action under. If ``log_under`` is not specified the vanila method is executed whic makes this manager comptabiile with code based on Default Manager API. Example Usage: import LoginManager class TeamRoster: ... objects = LoginManager() ... with loging: TeamRoster.object.get_or_create(log_under=request.user, competition=comp, team=team_to_add, defaults={'status': 50}) without: TeamRoster.object.get_or_create(competition=comp, team=team_to_add, defaults={'status': 50}) Extended methods so far: get_or_create() """ use_for_related_fields = True def get_or_create(self, loguser, **kwargs): result = super(LogingManager, self).get_or_create(**kwargs) created = result[1] object = result[0] user = loguser print 'here' if created: LogEntry.objects.log_action( user_id = user.pk, content_type_id = ContentType.objects.get_for_model(object).pk, object_id = self.model.pk, object_repr = force_unicode(self.model), action_flag = ADDITION, change_message = '%s object was added with kwargs: %s' % (str(user), str(**kwargs)) ) return result class FOO(models.Model): name = models.CharField(max_length=20) objects = LogingManager() class BAR(models.Model): foo = models.ForeignKey(FOO) objects = LogingManager()
Please change reported by to my e-mail: danols@…