Questions tagged [django-managers]

A Manager is the interface through which database query operations are provided to Django models.

A Manager is the interface through which database query operations are provided to Django models.

Source: https://docs.djangoproject.com/en/dev/topics/db/managers/

251 questions
151
votes
14 answers

How to Unit test with different settings in Django?

Is there any simple mechanism for overriding Django settings for a unit test? I have a manager on one of my models that returns a specific number of the latest objects. The number of objects it returns is defined by a NUM_LATEST setting. This has…
Soviut
  • 88,194
  • 49
  • 192
  • 260
110
votes
7 answers

Manager isn't accessible via model instances

I'm trying to get model objects instance in another one and I raise this error : Manager isn't accessible via topic instance Here's my model : class forum(models.Model): # Some attributs class topic(models.Model): # Some attributs class…
ThomasDurin
  • 1,943
  • 2
  • 14
  • 20
96
votes
2 answers

When should I use a custom Manager versus a custom QuerySet in Django?

In Django, custom managers are a great way to organize reusable query logic. The Django documentation on Custom managers says: There are two reasons you might want to customize a Manager: to add extra Manager methods, and/or to modify the initial…
John Lehmann
  • 7,975
  • 4
  • 58
  • 71
75
votes
8 answers

Custom QuerySet and Manager without breaking DRY?

I'm trying to find a way to implement both a custom QuerySet and a custom Manager without breaking DRY. This is what I have so far: class MyInquiryManager(models.Manager): def for_user(self, user): return self.get_query_set().filter( …
Jack M.
  • 30,350
  • 7
  • 55
  • 67
67
votes
3 answers

Django ORM - objects.filter() vs. objects.all().filter() - which one is preferred?

Very often I see constructs like MyModel.objects.all().filter(...) which will return a QuerySet of the default Mananger. At first all() seems to be quite redundant, because MyMode.objects.filter(...) delivers the same result. However, this seems…
Dr.Elch
  • 2,105
  • 2
  • 17
  • 23
57
votes
1 answer

select_related with reverse foreign keys

I have two Models in Django. The first has the hierarchy of what job functions (positions) report to which other positions, and the second is people and what job function they hold. class PositionHierarchy(model.Model): pcn =…
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
50
votes
4 answers

How to use custom manager with related objects?

I have a custom manager. I want to use it for related objects. I found use_for_related_fields in docs. But it does not work the way I used it: class RandomQueryset(models.query.QuerySet): def randomize(self): count =…
I159
  • 29,741
  • 31
  • 97
  • 132
39
votes
3 answers

Django custom managers - how do I return only objects created by the logged-in user?

I want to overwrite the custom objects model manager to only return objects a specific user created. Admin users should still return all objects using the objects model manager. Now I have found an approach that could work. They propose to create…
Thomas Kremmel
  • 14,575
  • 26
  • 108
  • 177
35
votes
4 answers

AttributeError: 'Manager' object has no attribute 'get_by_natural_key' error in Django?

I am using Django '1.5c1'. I have this line in my settings.py: AUTH_USER_MODEL = 'fileupload.galaxyuser' Here's my Galaxyuser model: class GalaxyUser(models.Model): id = models.IntegerField(primary_key=True) create_time =…
pynovice
  • 7,424
  • 25
  • 69
  • 109
31
votes
5 answers

Django Manager Chaining

I was wondering if it was possible (and, if so, how) to chain together multiple managers to produce a query set that is affected by both of the individual managers. I'll explain the specific example that I'm working on: I have multiple abstract…
Adam
  • 7,067
  • 2
  • 27
  • 24
25
votes
4 answers

Where should django manager code live?

This is a pretty simple django patterns question. My manager code usually lives in models.py, but what happens when models.py is really huge? Is there any other alternative pattern to letting your manager code live in models.py for maintainability…
eculver
  • 497
  • 1
  • 5
  • 9
19
votes
2 answers

How does use_for_related_fields work in Django?

I'm unable to grasp that from the docs. It's totally unclear to me, more specifically: Is it a global setting? So if I specify this attribute it on one of the model managers, will it be used globally by all the model classes? If it's not a global…
julx
  • 8,694
  • 6
  • 47
  • 86
17
votes
5 answers

How to use custom managers in chain queries?

I made a custom manager that has to randomize my query: class RandomManager(models.Manager): def randomize(self): count = self.aggregate(count=Count('id'))['count'] random_index = random.randint(0, count - 1) …
I159
  • 29,741
  • 31
  • 97
  • 132
13
votes
1 answer

Django custom model managers

I'm confused about the correct way to use Django custom model managers - based on the docs you can create a series of managers for one model as a way of filtering. But why not create one manager class with a series of functions for filtering? Is one…
9-bits
  • 10,395
  • 21
  • 61
  • 83
13
votes
1 answer

override Django get_or_create

I have a model that I overrode the save method for, so that the save method can be passed in some data and auto-fill-in a field before saving. Here is my model: class AccountModel(models.Model): account = models.ForeignKey(Account) def…
lovefaithswing
  • 1,510
  • 1
  • 21
  • 37
1
2 3
16 17