Redirect on admin Save

To change the redirect destination after save in the admin, you need to override response_add() (for adding new instances) and response_change() (for changing existing ones) in the ModelAdmin class. See the original code in django.contrib.admin.options. Quick examples to make it clearer how to do this (would be within a ModelAdmin class): from django.core.urlresolvers import reverse … Read more

Order by count of a ForeignKey field?

from django.db.models import Count top_users = User.objects.filter(problem_user=False) \ .annotate(num_submissions=Count(‘submission’)) \ .order_by(‘-num_submissions’)[:3] You didn’t mention problem_user in your example model code, but I’ve left it in assuming that it is a BooleanField on User.

Multiple images per Model

Variable lists, also known as a many-to-one relationship, are usually handled by making a separate model for the many and, in that model, using a ForeignKey to the “one”. There isn’t an app like this in django.contrib, but there are several external projects you can use, e.g. django-photologue which even has some support for viewing … Read more

List field in model?

You can convert it into string by using JSON and store it as string. For example, In [3]: json.dumps([[1, 3, 4], [4, 2, 6], [8, 12, 3], [3, 3, 9]]) Out[3]: ‘[[1, 3, 4], [4, 2, 6], [8, 12, 3], [3, 3, 9]]’ You can add a method into your class to convert it automatically … Read more

Django admin, hide a model

Based on x0nix’s answer I did some experiments. It seems like returning an empty dict from get_model_perms excludes the model from index.html, whilst still allowing you to edit instances directly. class MyModelAdmin(admin.ModelAdmin): def get_model_perms(self, request): “”” Return empty perms dict thus hiding the model from admin index. “”” return {} admin.site.register(MyModel, MyModelAdmin)

django unit tests without a db

You can subclass DjangoTestSuiteRunner and override setup_databases and teardown_databases methods to pass. Create a new settings file and set TEST_RUNNER to the new class you just created. Then when you’re running your test, specify your new settings file with –settings flag. Here is what I did: Create a custom test suit runner similar to this: … Read more