Questions tagged [django-custom-manager]

django-custom-manager refers to an ability to customize Django's default database Manager - an interface through which database query operations are provided to Django models

django-custom-manager refers to an ability to customize Django's default database Manager - an interface through which database query operations are provided to Django models.

See documentation.

43 questions
184
votes
3 answers

Django dynamic model fields

I'm working on a multi-tenanted application in which some users can define their own data fields (via the admin) to collect additional data in forms and report on the data. The latter bit makes JSONField not a great option, so instead I have the…
GDorn
  • 8,511
  • 6
  • 38
  • 37
84
votes
6 answers

Catching DoesNotExist exception in a custom manager in Django

I have a custom manager for a Django model. I don't seem to be able to catch DoesNotExist exception here. I know how to do it inside the model but it didn't work here: class TaskManager(models.Manager): def task_depend_tree(self, *args,…
Seperman
  • 4,254
  • 1
  • 28
  • 27
39
votes
3 answers

Django custom managers - how do I return only objects created by the logged-in user?

I want to overwrite the custom objects model manager to only return objects a specific user created. Admin users should still return all objects using the objects model manager. Now I have found an approach that could work. They propose to create…
Thomas Kremmel
  • 14,575
  • 26
  • 108
  • 177
17
votes
10 answers

TypeError: create_superuser() missing 1 required positional argument: 'profile_picture'

I get the following error after adding the profile_picture field: TypeError: create_superuser() missing 1 required positional argument: 'profile_picture' This profile_picture field is an "ImageField" set as "Null = True". I have tried the…
16
votes
3 answers

Can't login to django admin after creating a super user with a custom user model

I've been trying for hours to login to the django admin panel with a successfully created superuser but cannot get the right username/pw combo right. I want users to just use their email as their username. I've also done my best to replicate the…
7
votes
2 answers

Calling custom manager function in django templates

So I'm making an expense sheet django app and I'm stuck trying to render the Sum of all the inputted expenses. I've created a custom manager to calculate the sum: class ExpenseManager(models.Manager): def price_sum(self): return…
user2084044
5
votes
0 answers

AttributeError calling custom manager method in migration

I'm using Django 1.10.3, and in a migration step I encounter an error when I use RunPython() to call a custom manager method. Any ideas what I'm doing incorrectly? The error message is: AttributeError: 'Manager' object has no attribute…
Mike
  • 150
  • 1
  • 8
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
3
votes
1 answer

Django extending objects manager raises 'NoneType' object has no attribute '_meta'

I am trying to create a custom objects manager as described https://docs.djangoproject.com/en/dev/topics/db/managers/#modifying-initial-manager-querysets I am doing something like this: # the model, say Alpha class MyManager(Manager): …
Mark
  • 18,730
  • 7
  • 107
  • 130
2
votes
3 answers

Facing issue in custom manager of django

I am trying to create a custom manager to retrieve all posts with the published status. New to managers!! Thank you in advance <3. models.py class PublishedManager(models.Model): def get_query_set(self): return super(PublishedManager,…
2
votes
1 answer

Username appearing in django migrations over custom User model with no username

I have written a custom User model, with no username. Still it appears in migrations as primary key and throws unique constraint error while adding new user. Here is my User Model Manager: class UserManager(BaseUserManager): def create_user(self,…
2
votes
2 answers

Custom Manager in Django Destroys Caching for prefetch_related

If I do everything below without a custom manager, it all works as expected: class Content(models.Model): name = models.CharField(max_length=200) def __str__(self): return self.name class Meta: app_label = 'game' class…
2
votes
1 answer

django - use method on every object in queryset like filter '__in' custom manager

Hello I would like to implement method for player to take list of players (query set) and leave clan I'm looking for something like: Player.leave_clan([1,2,3]) Player.leave_clan([p1,p2,p3]) What I have tried is the method which takes list of…
oglop
  • 1,175
  • 1
  • 9
  • 15
2
votes
1 answer

How to solve error raised : No module named 'django.contrib.customuser'

I am new to Django, trying to create a custom user for my project. When I am running the server, it raises No module named 'django.contrib.customuser' and sometimes, Manager isn't available; auth.User has been swapped for Mysite.CustomUser. Even i…
vanam
  • 135
  • 1
  • 4
  • 14
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…
1
2 3