#14327: Custom Manager not honored in related queries
---------------------------+------------------------------------------------
 Reporter:  anonymous      |       Owner:  nobody    
   Status:  new            |   Milestone:            
Component:  Uncategorized  |     Version:  1.2       
 Keywords:                 |       Stage:  Unreviewed
Has_patch:  0              |  
---------------------------+------------------------------------------------
 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()

 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/14327>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to