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
13
votes
2 answers

User manager methods create() and create_user()

I have encountered with some suspicious behavior of create() method of User object manager. Looks like password field isn't required for creating User object if you use this method. In result you get User with blank password. In case when you use…
sunprophit
  • 1,639
  • 4
  • 16
  • 39
12
votes
2 answers

Convert django RawQuerySet to Queryset

I have 2 Django models, ModelA with an ArrayField that is used to store a large list of primary key values (possibly 50k+ list) class ModelA(models.Model): pk_values = ArrayField(models.IntegerField()) class CustomManager(manager.Manager): …
mishbah
  • 5,487
  • 5
  • 25
  • 35
12
votes
1 answer

django, difference between _base_manager and objects

django internal code uses _base_manager instead of objects There is also _default_manager I'm more accustomed to using objects What's the difference?
eugene
  • 39,839
  • 68
  • 255
  • 489
12
votes
3 answers

Django ORM. Joining subquery

I have a table which contains list of some web sites and a table with statistics of them. class Site(models.Model): domain_name = models.CharField( max_length=256, unique=True, ) class Stats(models.Model): date =…
psln
  • 121
  • 1
  • 1
  • 4
12
votes
1 answer

import RelatedManager from django.db.models.fields.related

I'm trying to do this: from django.db.models.fields.related import RelatedManager because I want to be able to test if an object is a related manager ie: isinstance(obj, RelatedManager) however I keep getting this error: Error: cannot import name…
9-bits
  • 10,395
  • 21
  • 61
  • 83
11
votes
1 answer

Django Model vs. Manager

Not really sure what the difference is. Seems like all the Manager does is have a bunch of functions related to the Model. But these functions could also be placed in the Model too.... Django documentation describes the Manager as follows, A…
reedvoid
  • 1,203
  • 3
  • 18
  • 34
10
votes
1 answer

Why define create_foo() in a Django models.Manager instead of overriding create()?

Reading the Django docs, it advices to make a custom creation method for a model named Foo by defining it as create_foo in the manager: class BookManager(models.Manager): def create_book(self, title): book = self.create(title=title) …
ruohola
  • 21,987
  • 6
  • 62
  • 97
10
votes
2 answers

Difference between UniqueConstraint vs unique_together - Django 2.2?

I started the new project in Django using version 2.2,which has new constraint unique constraint, Does this same as unique_together or it has any other differences?
Hari
  • 1,545
  • 1
  • 22
  • 45
10
votes
1 answer

Django - Runtime database switching

In my work we want to run a server with multiple databases. The databases switching should occur when you acces a url like http://myapp.webpage.com or http://other.webpage.com. We want to run only one server instance and at the moment of the HTTP…
10
votes
1 answer

django custom manager with filter parameter

I would like to add a custom manager which can be called from a template, but does not affect the entire model (e.g. admin views) and which listens to a parameter set in the request (user_profile). The following is what I have so…
szeta
  • 589
  • 1
  • 5
  • 21
8
votes
1 answer

How could i use custom manager of the related model, when using select_related?

For example, I have models as, class ModelBManager(models.Manager): def get_queryset(self): return self.super().get_queryset().select_related('y') class ModelA(models.Model): x = models.TextField() class ModelB(models.Model): y…
8
votes
2 answers

Creating a django manager with a parameter

I have the following situation I have a manager class that filters a queryset according to a field. The problem is that the field name is different according to the class but the value to which it filters comes from the same place (so i thought i…
Mr T.
  • 4,278
  • 9
  • 44
  • 61
8
votes
1 answer

Custom Manager to filter objects on site but not in admin?

I followed this example and it works great but I'm wondering if I can put in an exception so that when I am in the admin all objects show up (active and inactive). This might be simple but I can't find how to do it in the docs. Here's what my…
knuckfubuck
  • 969
  • 12
  • 25
8
votes
2 answers

Django: "Unknown Column" when run makemigrations on an app after adding an Integer Field

First of all, I have recently upgraded from Django 1.6 to 1.8, and never used South, so I'm new to migrations. I just ran: > python manage.py makemigrations myapp > python manage.py migrate --fake initial to create the initial migrations for my…
7
votes
2 answers

Always Defer a Field in Django

How do I make a field on a Django model deferred for all queries of that model without needing to put a defer on every query? Research This was requested as a feature in 2014 and rejected in 2022. Baring such a feature native to Django, the obvious…
Zags
  • 37,389
  • 14
  • 105
  • 140
1
2
3
16 17