Questions tagged [django-managers]
A Manager is the interface through which database query operations are provided to Django models.
238
questions
0
votes
1
answer
16
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
26
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
...
0
votes
1
answer
32
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)
...
2
votes
1
answer
45
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
43
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()...
1
vote
2
answers
92
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
68
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....
1
vote
1
answer
192
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):
...
0
votes
0
answers
103
views
Django Manager and QuerySets: How may I set up my manager and query to retrieve Parent ForeignKey ID in models.Queryset to build a folder directory?
How can I extract Parent ForeignKey IDs of ManyToManyFields by a Manager to build a path that i may a build Folder Directory?
I can query in the shell to retrieve the parent ID. Output is Board.id = 1 ...
1
vote
2
answers
44
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:
...
0
votes
1
answer
62
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 ...
1
vote
0
answers
214
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
66
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
36
views
QuerySet Manager django problem with 'objects'
I need to create 2 different ModelManager which inherits from my own Manager 'models.QuerySet'
I created :
class ActiveObjectsQuerySet(models.QuerySet):
def filter_active(self):
return ...
0
votes
0
answers
125
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
41
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
42
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
26
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
95
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
123
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
97
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
245
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
252
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
119
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
518
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
277
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 ...
2
votes
1
answer
261
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: ...
0
votes
3
answers
174
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
196
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
128
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
42
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
288
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
...
2
votes
2
answers
1k
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
50
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()...
0
votes
3
answers
1k
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
38
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
73
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....
1
vote
0
answers
334
views
Question: Object of type 'datetime' is not JSON serializable
Facing this issue file querying data from database, I am not using serializer, but using cursor in raw query on manager as you see.
view.py
@login_required
def index(request):
if not request.user....
1
vote
1
answer
272
views
How to Add custom manager to third-party app's model
I have added is_active field in all my models for adding soft-deleting functionality and added a custom manager for fetching active objects
class SoftDeleteManager(models.Manager):
def ...
1
vote
1
answer
30
views
Django custom model manager
If we have model like
class SomeModel(models.Model):
field_1 = models.IntegerField()
field_2 = models.IntegerField()
and in every query for a given value if we have to check against both the ...
10
votes
1
answer
325
views
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, ...
0
votes
1
answer
234
views
Custom Django Model Managers giving error when trying to use it
I'm trying to write a simple model manager that will filter on a particular field.
I have a model which looks like this:
"""Model definition for Period."""
year = models.ForeignKey(Year, ...
2
votes
1
answer
1k
views
How to pass a request.user to a model Manager?
I have a model manager with a get_queryset:
class BookManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(author=self.request.user
This results in the ...
3
votes
1
answer
1k
views
How to inherit multiple queryset filters via mixins
I have a QuerySetMixin in a model manager:
models.py:
class MyModel(models.Model):
objects = SoftDeletableManager()
managers.py:
class SoftDeletableManager(SoftDeletableManagerMixin, models....
0
votes
1
answer
75
views
When to use a custom model manager?
Right now i'm reading about custom managers that you can use to add additional logic when doing a CRUD action like Create. You make a custom manager class and then initialize the objects attribute of ...
2
votes
2
answers
136
views
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):
...
...
1
vote
1
answer
891
views
django migrations test: 'Manager' object has no attribute error
I'm testing a django migration inspired by this article. This method/ the MigrationTestCase works fine outside the user model, but seems to fall down when trying to access my custom user model.
Here ...
1
vote
2
answers
2k
views
Django custom reverse manager using a through model
I have a set of models where a Student is assigned multiple Classes via another model, ClassStudentMapping, which has a field where I can set which classes a student has on a particular day.
models....
0
votes
0
answers
39
views
Update a Model in Django right before the backend select
Before my Model in Django gets hydrated and filled with data, I want to update (alter) my model, save it back to the database and then go on the normal way.
My approach (not working) as now is this:
...
1
vote
3
answers
699
views
Django manager with datetime.timedelta object inside F query combined with annotate and filter
I am trying to create manager method inside my app, to filter emails object, that have been created 5/10/15 minutes or what so ever, counting exactly from now.
I though I'am gonna use annotate to ...