4

I want to get the related object references and I want to use a custom manager.

Is there something outside? How I can use a custom manager to get these objects?

 b.entry_set.all()

E.g

b.custom_manager.entry_set.all()
b.entry_custom_manager_set.all()
Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Azd325
  • 5,752
  • 5
  • 34
  • 57

2 Answers2

3

This has been possible since Django 1.7. Say you have a model Entry with two managers:

class Entry(models.Model):
    blog = models.ForeignKey('Blog', on_delete=models.CASCADE)
    ...
    
    objects = models.Manager()  # Default Manager
    entries = EntryManager()    # Custom Manager

Then you can control which manager you use by passing manager to entry_set:

b = Blog.objects.get(id=1)
b.entry_set.all() # implicitly use the default manager
b.entry_set(manager='objects').all() # explicitly use the default manager
b.entry_set(manager='entries').all() # explicitly use the entries manager

See the docs on using a custom reverse manager for more info.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
2

It looks like a ticket is still open for this feature https://code.djangoproject.com/ticket/3871

Ngenator
  • 10,909
  • 4
  • 41
  • 46
  • No problem. Keep an eye on future release notes, it looks like this might have been patched but not added to the 1.5 release due to a feature freeze so it might be in 1.6 – Ngenator May 01 '13 at 13:51