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)

Unique BooleanField value in Django?

Whenever I’ve needed to accomplish this task, what I’ve done is override the save method for the model and have it check if any other model has the flag already set (and turn it off). class Character(models.Model): name = models.CharField(max_length=255) is_the_chosen_one = models.BooleanField() def save(self, *args, **kwargs): if self.is_the_chosen_one: try: temp = Character.objects.get(is_the_chosen_one=True) if self … Read more

Django-Admin: CharField as TextArea

You will have to create a forms.ModelForm that will describe how you want the descr field to be displayed, and then tell admin.ModelAdmin to use that form. For example: from django import forms class CabModelForm( forms.ModelForm ): descr = forms.CharField( widget=forms.Textarea ) class Meta: model = Cab class Cab_Admin( admin.ModelAdmin ): form = CabModelForm The … Read more

Overriding admin css in django

It depends a lot of what you want to do. Though first of all: do not overwrite it in the Django admin directly. You got two options I think are reasonable: If you want to change the appearance of the admin in general you should override admin templates. This is covered in details here: Overriding … Read more

Default filter in Django admin

In order to achieve this and have a usable ‘All’ link in your sidebar (ie one that shows all rather than showing pending), you’d need to create a custom list filter, inheriting from django.contrib.admin.filters.SimpleListFilter and filtering on ‘pending’ by default. Something along these lines should work: from datetime import date from django.utils.translation import ugettext_lazy as … Read more

How to drop all tables from the database with manage.py CLI in Django?

As far as I know there is no management command to drop all tables. If you don’t mind hacking Python you can write your own custom command to do that. You may find the sqlclear option interesting. Documentation says that ./manage.py sqlclear Prints the DROP TABLE SQL statements for the given app name(s). Update: Shamelessly … Read more

Tying in to Django Admin’s Model History

The admin history is just an app like any other Django app, with the exception being special placement on the admin site. The model is in django.contrib.admin.models.LogEntry. When a user makes a change, add to the log like this (stolen shamelessly from contrib/admin/options.py: from django.utils.encoding import force_unicode from django.contrib.contenttypes.models import ContentType from django.contrib.admin.models import LogEntry, … Read more

A Better Django Admin ManyToMany Field Widget

Try using the filter_horizontal attribute on your admin class, for example: class SomeModelAdmin(admin.ModelAdmin): filter_horizontal = (‘users’,) As mentioned in the documentation, “adding a ManyToManyField to this list will instead use a nifty unobtrusive JavaScript “filter” interface that allows searching within the options”. filter_vertical does the same thing with a slightly different layout.

DateTimeField doesn’t show in admin system

If you really want to see date in the admin panel, you can add readonly_fields in admin.py: class RatingAdmin(admin.ModelAdmin): readonly_fields = (‘date’,) admin.site.register(Rating,RatingAdmin) Any field you specify will be added last after the editable fields. To control the order you can use the fields options. Additional information is available from the Django docs.

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