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
4
votes
1 answer

Django and the domain layer

How to organize my domain layer using django? I know I can write custom managers to hold my queries but what if I want something more flexible like the specification pattern. Is there any domain patterns that are unique to Django? I am…
the_drow
  • 18,571
  • 25
  • 126
  • 193
4
votes
1 answer

Single custom Manager for Multiple models in Django

I have several models connected to each other with ForeignKeys relationships. The main one in this sort of hierarchy contains a owner field. I would like to create a single custom manager for all these models that changes the returned queryset…
Leonardo
  • 4,046
  • 5
  • 44
  • 85
4
votes
2 answers

Combining django Managers

I have two django managers Vote manager class VoteManager(model.Manager): def all_with_vote_info(self): qs = super(VoteManager, self).get_query_set() qs = qs.annotate(score=Sum('votes__score', distinct=True)) return qs …
rolnn
  • 928
  • 9
  • 16
3
votes
1 answer

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( to='Hashtag', …
msln
  • 1,318
  • 2
  • 19
  • 38
3
votes
1 answer

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: middleware or custom function in manager (Both explained here…
danwos
  • 416
  • 3
  • 12
3
votes
1 answer

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() def…
3
votes
2 answers

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 error: AttributeError: 'BookManager' object has…
alias51
  • 8,178
  • 22
  • 94
  • 166
3
votes
1 answer

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.Manager): pass class…
alias51
  • 8,178
  • 22
  • 94
  • 166
3
votes
3 answers

Using Django custom manager function on an already filtered queryset

Consider the following case: class MyModelManager(models.Manager): def my_filter(self): return [some code here].filter(field_A__gt=3) class MyModel(models.Model): # some fields objects = MyModelManager() # The way I'd like to use…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
3
votes
1 answer

What's the difference between Custom model manager methods and QuerySet methods?

I am trying to get my head around writing Custom managers. I found the online documentation a little sparse. Toying around myself with code, I discovered the following patterns: Given the following model... class QuestionQuerySet(models.QuerySet): …
talkingtoaj
  • 848
  • 8
  • 27
3
votes
1 answer

Proper way to add record to many to many relationship in Django

First off, I'm planning on running my project on google app engine so I'm using djangoappengine which as far as I know doesn't support django's ManyToManyField type. Because of this I've setup my models like this: from django.db import models from…
Ian Burris
  • 6,325
  • 21
  • 59
  • 80
3
votes
1 answer

django-phone-number-field error on get_or_create method

I am getting an error on get_or_create method of the Django model that contains PhoneNumberField(django-phonenumber-field==1.3.0) field. It was working fine before, therefore I tried several older django-phonenumber-field(1.2.0, 1.1.0, 1.0.0)…
The Hog
  • 889
  • 10
  • 26
3
votes
4 answers

Django Migrations ValueError: Could not find manager in django.db.models.manager

I'm trying to update from Django 1.7 to Django 1.8 One of my models uses CurrentSiteManager from django.contrib.sites.managers like so: from django.contrib.sites.managers import CurrentSiteManager class NewsQuerySet(models.QuerySet): …
Todor
  • 15,307
  • 5
  • 55
  • 62
3
votes
1 answer

How to get the ManyToMany Manager object using the field name

I have a Django model with multiple ManyToMany fields. I want to query one of them dynamically, according to the field name, inside a method of this model. How do I get the ManyToMany manager object using only the field name. Is it possible?
arnon cohen
  • 485
  • 1
  • 5
  • 15
3
votes
2 answers

how to add a custom manager dynamically to model in django

In this django project, there are a lot of models and we want to add a custom queryset and a manager to each using the queryset.as_manager without having to specify it in each model by hand. All the queryset/managers are organized in a parallel…
eskhool
  • 657
  • 1
  • 11
  • 24