Django Manager Chaining

See this snippet on Djangosnippets: http://djangosnippets.org/snippets/734/ Instead of putting your custom methods in a manager, you subclass the queryset itself. It’s very easy and works perfectly. The only issue I’ve had is with model inheritance, you always have to define the manager in model subclasses (just: “objects = QuerySetManager()” in the subclass), even though they … Read more

AttributeError: ‘Manager’ object has no attribute ‘get_by_natural_key’ error in Django?

You have created a new user model but you have not yet specified a manager for that model. If you’re not yet familiar with managers in Django I suggest reading the documentation on that first. As the Django 1.5 say (source): You should also define a custom manager for your User model. If your User … Read more

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

One way to handle this would be to create a new method instead of redefining get_query_set. Something along the lines of: class UserContactManager(models.Manager): def for_user(self, user): return super(UserContactManager, self).get_query_set().filter(creator=user) class UserContact(models.Model): […] objects = UserContactManager() This allows your view to look like this: contacts = Contact.objects.for_user(request.user) This should help keep your view simple, and because … Read more

How to use custom manager with related objects?

For completeness of the topic, Django 1.7 (finally) supports using a custom reverse manager, so you can do something like that (just copying from the django docs): from django.db import models class Entry(models.Model): objects = models.Manager() # Default Manager entries = EntryManager() # Custom Manager b = Blog.objects.get(id=1) b.entry_set(manager=”entries”).all()

select_related with reverse foreign keys

Yes, that is what prefetch_related() is for. It will require an additional query, but the idea is that it will get all of the related information at once, instead of once per Person. In your case: qs.select_related(‘position__report_to’) .prefetch_related(‘position__report_to__person_set’) should require two queries, regardless of the number of Persons in the original query set. Compare this … Read more

Django ORM – objects.filter() vs. objects.all().filter() – which one is preferred?

The method all() on a manager just delegates to get_queryset(), as you can see in the Django source code: def all(self): return self.get_queryset() So it’s just a way to get the QuerySet from the Manager. This can be handy to ensure that you’re dealing with a QuerySet and not a Manager, because MyModel.objects returns a … Read more

Custom QuerySet and Manager without breaking DRY?

The Django 1.7 released a new and simple way to create combined queryset and model manager: class InquiryQuerySet(models.QuerySet): def for_user(self, user): return self.filter( Q(assigned_to_user=user) | Q(assigned_to_group__in=user.groups.all()) ) class Inquiry(models.Model): objects = InqueryQuerySet.as_manager() See Creating Manager with QuerySet methods for more details.

How to Unit test with different settings in Django?

EDIT: This answer applies if you want to change settings for a small number of specific tests. Since Django 1.4, there are ways to override settings during tests: https://docs.djangoproject.com/en/stable/topics/testing/tools/#overriding-settings TestCase will have a self.settings context manager, and there will also be an @override_settings decorator that can be applied to either a test method or a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)