Opened 5 years ago

Closed 5 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: UI/UX:


Dear awesome Django team,

I believe the below is a bug based on the documentation:

In a gist if I have a custom manager for FOO that overwrites get_or_create() this will work:


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
	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})
    TeamRoster.object.get_or_create(competition=comp, team=team_to_add, defaults={'status': 50})
	Extended methods so far:
	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:
            user_id         =, 
            content_type_id = ContentType.objects.get_for_model(object).pk,
            object_id       =,
            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()

Change History (2)

comment:1 Changed 5 years ago by danols@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Please change reported by to my e-mail: danols@…

comment:2 Changed 5 years ago by mk

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #3871

Note: See TracTickets for help on using tickets.
Back to Top