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

Django : how could I overload Q in django.db.models.query QuerySet to use for a special purpose in my Manager

First of all, since this is my first question/post, I would like to thank you all for this great community, and amazing service, as -- like many developers around the world -- stackoverflow -- is my main resource when it comes to code issues. Notice…
3
votes
1 answer

how does django assign the foreign key when using the create method of a related object manager?

How does Django assign the foreign key when using the create method of a related object manager? I'm asking because when I create my own create method, the foreign key is not set automatically. Here is an example: class…
Michael
  • 8,357
  • 20
  • 58
  • 86
3
votes
1 answer

How to pass parameteres to get_queryset method of django models manager?

I am trying to follow this documentation. I would like to pass somehow parameters to get_queryset, but don't know how. Not working demo below. class ContextAwareNotificationsManager(models.Manager): def get_queryset(self, user): return…
andilabs
  • 22,159
  • 14
  • 114
  • 151
3
votes
1 answer

django access logged in user in custom manager

I want to access the currently logged in user in a custom manager I have wrote. I want to do this so that I can filter down results to only show objects they have access to. Is there anyway of doing this without actually passing it in? Something…
John
  • 21,047
  • 43
  • 114
  • 155
3
votes
4 answers

Django: How do you access a model's instance from inside a manager?

class SupercalifragilisticexpialidociousManager(models.Manager): # Sorry, I'm sick of Foo and Spam for now. def get_query_set(self, account=None): return super(SupercalifragilisticexpialidociousManager, …
orokusaki
  • 55,146
  • 59
  • 179
  • 257
3
votes
3 answers

Django: How would one organize this big model / manager / design mess?

To sum things up before I get into bad examples, et al: I'm trying to make an application where I don't have to write code in all my models to limit choices to the current logged in account (I'm not using Auth, or builtin features for the account or…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
3
votes
1 answer

django model ManyToManyField not going through Manager

I have 3 models. class Poll(model): title = models.CharField() options = models.ManyToManyField(Option, through='PollOption', null=True, blank=True) class Option(model): title = models.CharField() #also declared a manager,…
kirans_6891
  • 121
  • 9
2
votes
1 answer

In a Django manager, why use self.get_query_set().get(kwarg=val) rather than self.get(kwarg=val)?

In a prior question I asked, where a Manager's method looked like: def activate(key): try: profile = self.get(key=key) except self.model.DoesNotExist: return None if not profile.key_expired(): # -> Activate user …
jvc26
  • 6,363
  • 6
  • 46
  • 75
2
votes
3 answers

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 already has a superuser with which I can access…
2
votes
2 answers

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 ?
Skratt
  • 289
  • 4
  • 13
2
votes
1 answer

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): ... objects = UserManager() When I am…
skg_9
  • 59
  • 5
2
votes
0 answers

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 user manager. I placed a print statement in the…
2
votes
0 answers

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 Django community, but I can't find one, so would…
MDalt
  • 1,681
  • 2
  • 24
  • 46
2
votes
0 answers

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','field3') when I print querSet it give me…
AlASAD WAIL
  • 775
  • 4
  • 18
2
votes
2 answers

How to add a Manager from Field

What i want to do is when some model use my field, it will automaticaly add custom manager to that model. As far as i know, contibute_to_class provide such functionality class MyCustomField(CharField): def contribute_to_class(self, cls, name): …
user20955
  • 2,552
  • 4
  • 24
  • 23