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
1
vote
1 answer

Django: exceptions and returns, what is the correct way to handle multiple potential situations?

I have a function in a Manager which activates a user account via a key. When a key is provided, several checks need to be performed: does the key exist? has the key expired? and then if not, the manager activates the account. def activate(key): …
jvc26
  • 6,363
  • 6
  • 46
  • 75
1
vote
1 answer

How to use Django Custom Model Managers

How do you determine whether something should be a separate custom model manager or a function of an existing model manager? For example I could create a single model manager whose queryset is all instances of the model. Then I could create…
9-bits
  • 10,395
  • 21
  • 61
  • 83
1
vote
1 answer

Performing a ModelAdmin action in Django

I'd like to be able to do something like: # from the docs def make_published(modeladmin, request, queryset): queryset.update(status='p') But I'm not using the Django admin site - I just need to be able to do this type of functionality elsewhere…
9-bits
  • 10,395
  • 21
  • 61
  • 83
1
vote
0 answers

Overriding the default validation for a Django admin Foreign Key field

I have Django models that inherit from an Archive class in which the default manager (objects) is replaced with a query that does not return archived records. This works well throughout the system. However, in admin, when a record with a foreign…
1
vote
1 answer

How to get the data returned by the previous query in the method of custom class manager?

I developing a custom manager class with chainable method. Got a problem. I need to randomize filtered query. To get a random record I need a count of filtered and distinct records. But I don't know how to get it. On the contrary, I have a count of…
I159
  • 29,741
  • 31
  • 97
  • 132
1
vote
1 answer

Overriding create() method in the model manager doesn't do anything

I have a model employee, and while creating an instance I want to automatically set their email to ".@company.com". So, I wrote a manager to do the pre-processing: class EmployeeManager(models.Manager): def create(self,…
musical_ant
  • 143
  • 2
  • 7
1
vote
1 answer

in _validate_username AttributeError: 'Manager' object has no attribute 'get_by_natural_key' error

models.py: from django.db import models from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin from .managers import UserAccountManager class UserAccount(AbstractBaseUser, PermissionsMixin): email =…
1
vote
1 answer

Custom model manager for auth_user

I want to include two extra managers on the auth user model, active and inactive, to give me just active, or just inactive users. This is how the model would look (even if the it is invalid): from django.contrib.auth.models import User class…
David542
  • 104,438
  • 178
  • 489
  • 842
1
vote
1 answer

Django custom model manager for derived class - filtering on base class attribute

I am using Django 3.2 I have the following models: class AuthoriseableApprovedManager(models.Manager): def get_queryset(self): return super(AuthoriseablePendingManager,…
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
1
vote
1 answer

Filter queryset accouring to related objects in django

I used django-guardian library, I want to create Manager to filter objects according to user permission. So for-example: from guardian.shortcuts import get_objects_for_user class WithUser(models.Manager): user_obj = None def…
1
vote
2 answers

Object creation assigns proxy class based on base model field(s)

I'm hoping to be able to create an object using a base model but have that object actually be created as a proxy class depending on the object's field(s). So for example, for the following models: class Animal(models.Model): species =…
Chris
  • 500
  • 6
  • 25
1
vote
1 answer

Django custom queryset with pre-filter

Is there a way in django to write a custom queryset class that has a filter "pre-applied" without having to call a method on it? I want to create a manager using QuerySet.as_manager(). But I want that manager to automatically filter out some…
samfrances
  • 3,405
  • 3
  • 25
  • 40
1
vote
2 answers

Struggling to see the utility of custom Django Model Managers

I do not have any code for this question, so this will be more about the utility of customer Managers, more so than it is an implementation question. I have read the documentation, many blog posts and tried to implement some myself, but I cannot…
jeremy_lord
  • 469
  • 7
  • 23
1
vote
2 answers

What functions do the order of Django Managers affect?

So, I've read most of the docs and I've been looking around on SO a bit, but I can't quite find the answer to my question. I'll start with the code. # Manager class ActiveManager(models.Manager): def get_query_set(self): return…
Bryce Siedschlaw
  • 4,136
  • 1
  • 24
  • 36
1
vote
1 answer

When I define a custom manager .. Error:Manager isn't accessible via Post instances

I defined a custom manager inheriting models. Manager put in in my model 'Post'. The error says that you cant call manager through a instance but i have not called it through a instance it works fine when i remove the custom manager. models.py: …