Some useful Django admin customization and djangoadmin Customization

Source: Internet
Author: User

Some useful Django admin customization and djangoadmin Customization

Model instance, myapp/models. py:

from django.db import modelsclass Blog(models.Model):    name = models.CharField(max_length=100)    tagline = models.TextField()     # On Python 3: def __str__(self):    def __unicode__(self):        return self.name class Author(models.Model):    name = models.CharField(max_length=50)    email = models.EmailField()     # On Python 3: def __str__(self):    def __unicode__(self):        return self.name class Entry(models.Model):    blog = models.ForeignKey(Blog)    headline = models.CharField(max_length=255)    body_text = models.TextField()    pub_date = models.DateField()    mod_date = models.DateField()    authors = models.ManyToManyField(Author)    n_comments = models.IntegerField()    n_pingbacks = models.IntegerField()    rating = models.IntegerField()     # On Python 3: def __str__(self):    def __unicode__(self):        return self.headline
Class-level Permissions

By default, superusers can access all models on the admin interface, but sometimes they only want some users to access some specific models.

You can customize the has_perm () method of your User object:

class MyUser(AbstractBaseUser):    ...    def has_perm(self, perm, obj=None):        if self.is_superuser:            return True        elif self.can_edit:            if perm=='myapp.add_entry':                return True            else:                return False        else:            return False

In this way, superuser has all permissions. When the can_edit attribute of a common user is True, it has the permission to create an Entry instance, and other users do not have the permission.

You can also customize the has_add_permission (), has_change_permission (), and has_delete_permission () Methods of ModelAdmin:

    def has_add_permission(self, request):        """        Returns True if the given request has permission to add an object.        Can be overridden by the user in subclasses.        """        opts = self.opts        codename = get_permission_codename('add', opts)        if request.user.can_edit:            return True        else:            return request.user.has_perm("%s.%s" % (opts.app_label, codename))
Field-level Permissions

You can edit different content with different permissions. You can use get_readonly_fileds () to add the field read-only permission.

class EntryAdmin(admin.ModelAdmin):    list_display=(...)    search_fields=(...)    def get_readonly_fields(self,request,obj=None):        if not request.user.is_superuser and not request.user.can_edit:            return [f.name for f in self.model._meta.fields]        return self.readonly_fields
Override the save action of the Model.

You can directly override the save () method of the model:

from django.db import modelsclass Blog(models.Model):    name = models.CharField(max_length=100)    tagline = models.TextField()    def save(self, *args, **kwargs):        do_something()        super(Blog, self).save(*args, **kwargs) # Call the "real" save() method.        do_something_else()

Block save ():

from django.db import modelsclass Blog(models.Model):    name = models.CharField(max_length=100)    tagline = models.TextField()    def save(self, *args, **kwargs):        if self.name == "Yoko Ono's blog":            return # Yoko shall never have her own blog!        else:            super(Blog, self).save(*args, **kwargs) # Call the "real" save() method.

You can also override the save_model () method of ModelAdmin to customize different save Behaviors Based on Different users:

from django.contrib import adminclass ArticleAdmin(admin.ModelAdmin):    def save_model(self, request, obj, form, change):        obj.user = request.user        obj.save()

Obj is the modified object, change = False when a new object is created, change = True when an object is modified, and get the object before modification:

from django.contrib import adminclass ArticleAdmin(admin.ModelAdmin):    def save_model(self, request, obj, form, change):        if change:            obj_old = self.model.objects.get(pk=obj.pk)        else:            obj_old = None        obj.user = request.user        obj.save()
Different users display different data rows. Rewrite the query set returned on the list page.

ModelAdmin provides a hook program namedqueryset()To determine the default query set returned on any list page.

class MyModelAdmin(admin.ModelAdmin):    def get_queryset(self, request):        qs = super(MyModelAdmin, self).get_queryset(request)        if request.user.is_superuser:            return qs        return qs.filter(author=request.user)
Custom filter list_filter

Inherit a subclass from django. contrib. admin. SimpleListFilter, provide the title and parameter_name attributes, and override the lookups and queryset methods.

from datetime import datefrom django.contrib import adminfrom django.utils.translation import ugettext_lazy as _class DecadeBornListFilter(admin.SimpleListFilter):    # Human-readable title which will be displayed in the    # right admin sidebar just above the filter options.    title = _('decade born')    # Parameter for the filter that will be used in the URL query.    parameter_name = 'decade'    def lookups(self, request, model_admin):        """        Returns a list of tuples. The first element in each        tuple is the coded value for the option that will        appear in the URL query. The second element is the        human-readable name for the option that will appear        in the right sidebar.        """        return (            ('80s', _('in the eighties')),            ('90s', _('in the nineties')),        )    def queryset(self, request, queryset):        """        Returns the filtered queryset based on the value        provided in the query string and retrievable via        `self.value()`.        """        # Compare the requested value (either '80s' or '90s')        # to decide how to filter the queryset.        if self.value() == '80s':            return queryset.filter(birthday__gte=date(1980, 1, 1),                                    birthday__lte=date(1989, 12, 31))        if self.value() == '90s':            return queryset.filter(birthday__gte=date(1990, 1, 1),                                    birthday__lte=date(1999, 12, 31))class PersonAdmin(admin.ModelAdmin):    list_filter = (DecadeBornListFilter,)

Parameter_name and title are required. The look_up method returns the options and descriptions that appear in the filter on the right of the List page. Parameter_name is the name of the get request appended to the url. self. value () returns the value of this parameter.

Customization Based on Different users:

class AuthDecadeBornListFilter(DecadeBornListFilter):    def lookups(self, request, model_admin):        if request.user.is_superuser:            return super(AuthDecadeBornListFilter,                self).lookups(request, model_admin)    def queryset(self, request, queryset):        if request.user.is_superuser:            return super(AuthDecadeBornListFilter,                self).queryset(request, queryset)

Model_admin is a ModelAdmin instance:

class AdvancedDecadeBornListFilter(DecadeBornListFilter):    def lookups(self, request, model_admin):        """        Only show the lookups if there actually is        anyone born in the corresponding decades.        """        qs = model_admin.get_queryset(request)        if qs.filter(birthday__gte=date(1980, 1, 1),                      birthday__lte=date(1989, 12, 31)).exists():            yield ('80s', _('in the eighties'))        if qs.filter(birthday__gte=date(1990, 1, 1),                      birthday__lte=date(1999, 12, 31)).exists():            yield ('90s', _('in the nineties'))
Custom Search
class PersonAdmin(admin.ModelAdmin):    list_display = ('name', 'age')    search_fields = ('name',)    def get_search_results(self, request, queryset, search_term):        queryset, use_distinct = super(PersonAdmin, self).get_search_results(request, queryset, search_term)        try:            search_term_as_int = int(search_term)        except ValueError:            pass        else:            queryset |= self.model.objects.filter(age=search_term_as_int)        return queryset, use_distinct

Queryset is the query set and search_term is the search term.

Foreign key field filtering

When the foreign key option is displayed when an object is added, too many options are unfriendly. In this case, you need to filter out the objects that meet the requirements for selection.

class MyModelAdmin(admin.ModelAdmin):    def formfield_for_foreignkey(self, db_field, request, **kwargs):        if db_field.name == "car":            kwargs["queryset"] = Car.objects.filter(owner=request.user)        return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

  


In django, I wrote admin in the backend. Now I want to use user authentication. I feel that the self-built authentication function is better. How can I use it?

The built-in authentication function is really good, but I don't understand what you mean by using admin, and there are a lot of user authentication items, so I don't know where to start.

If request. user. is_authenticated ():
# Authenticated Users
Else:
# Anonymous Users

Reference: zh.wikibooks.org/..?af=81

Can django admin edit data?

The main idea is to build a WYSIWYG text editor.
I have never tried it. I searched it for help. It seems that the following is recommended.
Django-tinymce
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.