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):
super(MyCustomField, self).contribute_to_class(cls, name)
setattr(cls, 'c...
I was wondering if it was possible (and, if so, how) to chain together multiple managers to produce a query set that is affected by both of the individual managers. I'll explain the specific example that I'm working on:
I have multiple abstract model classes that I use to provide small, specific functionality to other models. Two of t...
Is there any simple mechanism for overriding Django settings for a unit test? I have a manager on one of my models that returns a specific number of the latest objects. The number of objects it returns is defined by a NUM_LATEST setting.
This has the potential to make my tests fail if someone were to change the setting. How can I ove...
Hi!
I have following model:
class UserProfile(models.Model):
"""
User profile model, cintains a Foreign Key, which links it to the
user profile.
"""
about = models.TextField(blank=True)
user = models.ForeignKey(User, unique=True)
ranking = models.IntegerField(default = 1)
avatar = models.ImageField(uploa...
I'm using the object_list generic view to quickly list a set of Articles. Each Article has comments attached to it. The query uses an annotation to Count() the number of comments and then order_by() that annotated number.
'queryset': Article.objects.annotate(comment_count=Count('comments')).order_by('-comment_count'),
The comments a...
This is a pretty simple django patterns question. My manager code usually lives in models.py, but what happens when models.py is really huge? Is there any other alternative pattern to letting your manager code live in models.py for maintainability and to avoid circular imports?
A question may be asked as to why models.py is so huge, but...
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 your own middleware looking like this:
#### myproject/middleware/threadlocals.py
try:
...
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 login).
ie, I don't want to have to do something like this:
class Ticket(models.Model):...
class SupercalifragilisticexpialidociousManager(models.Manager):
# Sorry, I'm sick of Foo and Spam for now.
def get_query_set(self, account=None):
return super(SupercalifragilisticexpialidociousManager,
self).get_query_set().filter(uncle=model_thats_using_this_manager_instance.uncle)
The magic I'm l...
class Account(models.Model):
identifier = models.CharField(max_length=5)
objects = MyCustomManager()
class Meta:
abstract = True
class Customer(Account):
name = models.CharField(max_length=255)
If I have a lot of models, and I want to save time from having to put foreignkeys everywhere, is ...
I need to have all my models inherit this manager (Haven't tested this manager and it may be ridiculous so I'm completely open to suggestion/criticism on it as well):
class AccountFilterManager(models.Manager):
def __init__(self, account=None, *args, **kwargs):
super(AccountFilterManager, self).__init__(*args, **kwargs)
...
I'm trying to find a way to implement both a custom QuerySet and a custom Manager without breaking DRY. This is what I have so far:
class MyInquiryManager(models.Manager):
def for_user(self, user):
return self.get_query_set().filter(
Q(assigned_to_user=user) |
Q(assigned_to_group__in=...
Hi,
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 similar to how it works in views where you can do request.user.
Thanks
...
Hi there!
I have a model, smth like this:
class Action(models.Model):
def can_be_applied(self, user):
#whatever
return True
and I want to override its default Manager. But I don't know how to pass the current user variable to the manager, so I have to do smth like this:
[act for act in Action.objects.all() if a...
I have 2 models:
class A(Model):
#Some Fields
objects = ClassAManager()
class B(A):
#Some B-specific fields
I would expect B.objects to give me access to an instance of ClassAManager, but this is not the case....
>>> A.objects
<app.managers.ClassAManager object at 0x103f8f290>
>>> B.objects
<django.db.models.manager.Mana...
I have been refactoring an app that had customized the standard User model from django.contrib.auth.models by creating a UserProfile and defining it with AUTH_PROFILE_MODULE.
The problem is the attributes in UserProfile are used throughout the project to determine what the User sees.
I had been creating tests and putting in this type ...
I have the following models code :
from django.db import models
from categories.models import Category
class MusicManager(models.Manager):
def get_query_set(self):
return super(MusicManager, self).get_query_set().filter(category='Music')
def count_music(self):
return self.all().count()
class SportManager(models...
Hi guys,
I'm writing a Project in Django where I've 5 kind of groups of Users:
Group1
Group2
...
Then I've a Model, Item which has many relation with users, the Item has one Owner (a User in Group1), a Customer (an User in Group2) and many RelatedUser (Users in Group3).
I'm wondering which is the correct way to write this relations...
I followed this example and it works great but I'm wondering if I can put in an exception so that when I am in the admin all objects show up (active and inactive). This might be simple but I can't find how to do it in the docs.
Here's what my manager looks like now:
class ShareManager(models.Manager):
def get_query_set(self):
...