Questions tagged [django-managers]
A Manager is the interface through which database query operations are provided to Django models.
251
questions
1
vote
0
answers
34
views
Relation does not exist error when overriding get_queryset method of models.Manager
I have a model for storing music and it has last_queried_at field that is used for deleting the music object if not queried for a month.
class CustomManager(models.Manager):
use_in_migrations = ...
0
votes
1
answer
16
views
Manager's annotations don't show up when traversing ForeignKey
Suppose I have such classes:
class UserManager(Manager):
def get_queryset(self):
return super().get_queryset().annotate(
full_name=Concat(
F('first_name'), ...
1
vote
0
answers
31
views
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.
...
0
votes
1
answer
146
views
Django model update multiple instances with custom Manager function
I have a model class (for example A) inside the Django model framework. For this model I have created his Manager class with some functions.
class T_Manager(BaseModelManager):
def add_function(...
0
votes
0
answers
45
views
Django custom models give [return-value] error
I am building some custom model fields for a project, which include a User model and a Team model, both in different directories. I have included the team app in the settings.
I included the import in ...
0
votes
1
answer
37
views
Django: In a ModelManager, how do I access a constant defined inside the related Model?
I have, for example, an Enum defined inside a Model.
That model uses, and therefore imports, a ModeLManager.
In that ModelManager, I'd like to access the Enum that I defined inside the Model; but I ...
0
votes
1
answer
67
views
ValueError: Related model 'users.customuser' cannot be resolved
Can't figure out what to do to solve this problem. I am trying to create a custom user model but when i try to migrate it throws this error.
Here is managers.py:
from django.contrib.auth.base_user ...
1
vote
2
answers
669
views
How to make Django values() exclude some fields?
I have multiple models which has fields like "created_at" "updated_at" which I don't want to get with objects.values().
Does Django has any way to exclude fields in values()?
I ...
1
vote
1
answer
377
views
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 "<first_name>.<last_name>@company.com". So, I wrote a manager to do the pre-...
7
votes
2
answers
2k
views
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.
...
0
votes
1
answer
401
views
FieldError: Cannot resolve keyword 'get_str' into field while using @property method in values_list() in a QuerySet
I have a model named Invitation, where I have created a property method which returns the value returned by __str__() method, like below:
class Invitation(models.Model):
def __str__(self):
...
0
votes
1
answer
494
views
AttributeError: 'RegisterPatientView' object has no attribute 'object'
When ever I am running the project and try to create register the patient or doctor it is showing this error. error
and below it is showing error in return HttpResponseRedirect(self.get_success_url())
...
1
vote
1
answer
460
views
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, ...
0
votes
0
answers
50
views
Can we add a method like create_user_with_permission just like create_user in django?
I'm making a django website and I had made groups for the admin page access(Admin, Content Writer, Users). What I want is when a user signs up they should be assigned the specific group based on from ...
0
votes
1
answer
156
views
Do DRF views use its queryset's object manager?
If I have a custom object manager with a custom create function for a model:
class CustomManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(custom=True)
def ...
0
votes
1
answer
133
views
Django: related_name issue
I am trying to make a query with related_name. I need to list tenants and its domain. But I am getting this error:
'TenantManager' object has no attribute 'domains'
What am I doing wrong?
models.py
...
2
votes
3
answers
2k
views
Django: Create a superuser in a data migration
Goal: automatically creating a superuser
I'm trying to create a default user, specifically a superuser, in an early data migration, so whenever my Django application is run in my Docker container, it ...
0
votes
1
answer
208
views
How do i modify my create function in Django model manager
So i have this model:
class Token(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=False)
code = models.IntegerField(default=code)
...
3
votes
1
answer
445
views
Django - How to set default ordering on ManyToMany field queryset
I have following models:
class Hashtag(models.Model):
...
created = models.DateTimeField(auto_now_add=True)
...
class Tweet(models.Model):
...
hashtags = models.ManyToManyField(
...
1
vote
1
answer
422
views
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, self).get_queryset()...
2
votes
2
answers
680
views
Django: how to test type of RelatedManager?
How to test the type of a RelatedManager in Django ?
assert type(qs) in [models.QuerySet, models.Manager]
fails if qs is a RelatedManager
how do I test if qs is a RelatedManager ?
1
vote
1
answer
123
views
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....
2
votes
1
answer
680
views
Django Custom User Manager is not called in admin panel
I am implementing a custom user model using AbstractBaseUser and BaseUserManaer .
class UserManager(BaseUserManager):
def create_user(self,username,password=None):
class User(AbstractBaseUser):
...
1
vote
2
answers
193
views
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:
...
1
vote
1
answer
903
views
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 ...
2
votes
0
answers
457
views
Django Rest Framework admin panel not using custom User Manager
I have implemented a CustomUser and CustomUserManager in my DRF app. The tutorials I have followed are this one and this one, which are nearly identical approaches to creating a custom user and custom ...
1
vote
2
answers
150
views
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 ...
0
votes
0
answers
680
views
'UserManager' object has no attribute 'validate'
My usual registration and login validators stopped working and I'm banging my head against the wall trying to figure out why.
This is the error:
AttributeError at /register
'UserManager' object has no ...
1
vote
1
answer
122
views
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 ...
0
votes
2
answers
126
views
Django creates two model instances instead of one
I'm trying to learn some django basics following one turorial from youtube and have got strange result when I try to create some model instances using forms. Django implicitly creates two duplicate ...
1
vote
0
answers
40
views
Overwrite Django "Using" method
I want to use PostgreSQL multi schema in Django.
I want to overwrite using method to make my query according schema name.
I want like this:
objs=MyModel.objects.using('schema_name')
objs.filter(...) # ...
0
votes
1
answer
239
views
how to seprately queryset admin django and rest api by model manger?
By code below in admin panel query set return only rows that is_deleted is "false" if I want to return all rows. I have one idea but not sure that is bests or does not have a bug.
all models ...
0
votes
1
answer
299
views
How to filter draft contents from django website?
I have two options for my articles in my django website: "draft" and "published"
I wrote sth that helps me show only the articles that are on "published" status in admin ...
2
votes
0
answers
233
views
Simple caching mechanism for Django manager functions
I have some Django simple manager functions where I'd like to cache the response (using Memcached), and invalidate these on model save/delete. I thought there'd be a standard solution for this in the ...
0
votes
1
answer
429
views
How to add a calculated field to a django query expression
I have a Django model, DocumentComments, with two datetime fields, created and updated. I am working on a search function that parses a search string and returns a Q expression to query the ...
2
votes
0
answers
335
views
Create custom Values method in django
I want to create a method semi to values method in Django QuerySet.
The values method problems are:
Miss order of fields in querySet if I make myquery = MyModel.objects.values('field1','field2','...
0
votes
1
answer
216
views
Recursive relationship in Django: get all the interrelated records from each record
I have this model for a tune:
class MsTune(models.Model):
name = models.CharField(max_length=255)
ms = models.ForeignKey(Manuscript, on_delete=models.CASCADE, related_name="mstunes")...
1
vote
1
answer
1k
views
Is it possible to override filter lookup with predefined values with custom Manager/Queryset in Django
I am trying to implement the behaviour for my models so that when you are deleting the object(s) it is not deleting physically, but just add some attribute to state that it was deleted.
So I created ...
1
vote
1
answer
587
views
Cannot resolve keyword 'get_album_detail' into field
Am trying to create a search field that can get the artist name and the name of the song, but am getting an error saying "Cannot resolve keyword 'get_full_album_detail' into field." once i ...
3
votes
1
answer
365
views
Django: Custom default Manager with user based queries for all views/interfaces (custom, admin, graphene, rest, ...)
In Django I'm trying to implement some kind of a "security middleware", which gives access to certain db information only, if the logged in user matches.
Up to now I found two approaches: ...
1
vote
3
answers
296
views
How to write manager class which use filter field as computed field not as a part of model fields?
I have a model Student with manager StudentManager as given below. As property gives the last date by adding college_duration in join_date. But when I execute this property computation is working well,...
0
votes
1
answer
323
views
pytest: how to avoid repetition in custom User manager testing
I'm testing a custom user manager with pytest and factory_boy. I want to test those cases where information required to create a new user is incomplete, but I have different required parameters at the ...
0
votes
0
answers
249
views
django change the default query set based on the requesting user
I have a system with multiple organizations logging in and interacting with us and our partners. I have a table that keeps track of what users have access to what organizations. I would like for ...
1
vote
1
answer
91
views
How to write manager class to set default attribute value for models while retrieval/filtering in Django queries?
class StudentModelManager(models.Manager):
def get_queryset(self, *args, **kwargs):
return (
super(StudentModelManager, self)
.get_queryset()
.filter(...
0
votes
1
answer
556
views
How to use default manager in django migrations with inherited models?
I have a model Baz that's inheriting from an abstract model Bar which is inheriting as well from an other abstract model Foo.
from django.db import models
class BarManager(models.Manager):
pass
...
4
votes
2
answers
2k
views
Django manager queries don't work in a chain of queries. AttributeError: 'QuerySet' object has no attribute <the manager method>
Problem: I've implemented a custom manager for a model with just one custom query set named get_by_tag and it's working fine if I use it this way:
ViewStatistic.objects.get_by_tag('some-tag-name')....
3
votes
1
answer
101
views
Django models.Manager unable to access model
I have the following test which fails as it only inserts one row into the database where it should be inserting 100 rows
class QuestionsTest(TestCase):
def setUp(self):
self.fake = Faker()...
1
vote
4
answers
6k
views
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings
hi i create a custom manager class called usermanager in monage.py
to customise my login template for 2 types of user admin,entrepreneur
#!/usr/bin/env python
"""Django's command-line utility for ...
-1
votes
1
answer
103
views
Django-Filter over Managers
I've been stuck in this for several weeks now and I believe the answer is super simple but somehow I can't find it anywhere online. Which makes me think I'm going about it totally wrong.
All I want ...
0
votes
1
answer
126
views
Django manager in lookup fields
I have a SoftDeletableModel named Offer:
class Offer(SoftDeletableModel):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name='offers')
order = models....