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

django - Way to avoid custom manager for specific queries

I have the following issue: I am working on a grown project which uses the pattern of overriding the get_queryset() method in the manager. # Model declaration class MyModel(models.Model): ... objects = MyModelManager() # Manager…
Ron
  • 22,128
  • 31
  • 108
  • 206
2
votes
1 answer

How do I override the .update() method inside a custom manager class

How can I override the update() method of a django model inside a custom manager? I would like to modify the behavior of some methods(all(), update(), filter()) of a django model and I have tried to override using what my code down here suggests but…
Olfredos6
  • 808
  • 10
  • 19
2
votes
1 answer

How to restrict Model.objects.all() to data pertaining to one organization

I'm developing a web application that will store multiple Organizations, where each organization has its own users. An organization is identified by a domain (CharField), and users only see data pertaining to their own organization. For simplicity…
realnot
  • 721
  • 1
  • 11
  • 31
2
votes
1 answer

Non-default argument follows default argument

In the following code, I am receiving an error stating: Non-default argument follows default argument. Would anyone happen to know how to resolve this? class MyUserManager(BaseUserManager): def _create_user(self, email, first_name=None,…
jape
  • 2,861
  • 2
  • 26
  • 58
2
votes
2 answers

How to programatically get manager instance for model in Django

I have a custom manager added to model like that: class StandardManagerModel(models.Model): pass class PublishableManager(models.Manager): pass class Publishable(models.Model): published_objects = PublishableManager() This removes the…
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
2
votes
1 answer

Is it required to make a custom manger for a custom user model in django

I am making a custom user model using the AbstractBaseUser and PermissionsMixin by following these two tutorials (tutorial-1 and tutorial-2). This the model so far: class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField('email…
2
votes
1 answer

Django models filter by the human-readable value

Is possible to filter models by the human-readable value? #models.py class World(models.Model): COUNTRY_CHOICES = (('SP', 'Spain'), ('FR', 'France')) country=models.CharField(max_length=20, choices=COUNTRY_CHOICES, default=None) …
ddepablo
  • 677
  • 1
  • 5
  • 16
2
votes
1 answer

Django form override ForeignKey queryset

I was making a form for creation unpublished Artist instances and then adding Artwork to the artist before publishing. I have manager to hide published=False artists and do not know how to bypass this yet in ForeignKey. models.py class…
Feanor
  • 3,568
  • 5
  • 29
  • 49
2
votes
2 answers

Django: Is it a good idea to use an abstract base model in this situation?

class Account(models.Model): identifier = models.CharField(max_length=5) objects = MyCustomManager() class Meta: abstract = True class Customer(Account): name = models.CharField(max_length=255) If I…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
2
votes
1 answer

In Django custom user manager, difference between create_user and _create_user()?

I'm trying to work through the tutorials for a Django custom user manager. Most of them override create_user() and create_super_user() like the documentation says to do, but this tutorial leaves those two methods alone and instead overrides…
aris
  • 22,725
  • 1
  • 29
  • 33
2
votes
3 answers

Django manager return all entries for today - DateTimeField

How would I make a manager that will return all the entries in a model with todays date, the model field is a datetime field, not a datefield? timestamp = models.DateTimeField(auto_now=True) class ValidEntryManager(models.Manager): def…
Harry
  • 13,091
  • 29
  • 107
  • 167
2
votes
2 answers

Django: objects.raw() unable to fetch results despite SQL working in MySQL directly

I've written a function for Django which allows a user to enter a word or phrase, and fetch all the instances in a specified model where that instance has all those words appear in any order across a range of specified fields. I have chosen to use…
michaeljtbrooks
  • 162
  • 2
  • 11
2
votes
2 answers

Django: SQL Injection-proof managers.py

I have a managers.py file that uses the .extra() parameter to perform raw SQL queries. For example: class MyManager(models.Manager): def order_null_last(self, field): return super(DecisionManager, self).get_query_set()\ …
powlo
  • 2,538
  • 3
  • 28
  • 38
2
votes
2 answers

Using another model manager's code in a Django custom manager class

I have two models, say, Question and Topic. I am trying to add methods to the Question model's custom manager, e.g. some method that filters by Topic. I can't seem to use the other manager's code for this (cannot import Topic either, so I can't do…
ustun
  • 6,941
  • 5
  • 44
  • 57
1
vote
1 answer

Django Managers or a better plan

I'm just getting my hands on Django Managers and I've found myself doing this type of programming. I'm looking to see if there is a way to remove the obvious repetition.. I believe I need to define use get_query_set to refer to myself? To be very…
rh0dium
  • 6,811
  • 4
  • 46
  • 79