Nerd For Tech
Published in

Nerd For Tech

From Zero to Hero Django Admin: Advanced-Level ModelAdmin Class-2 (Part4)

Using Model Properties in Admin App, Adding Rich Text Editor

We will continue adding new properties to the ModelAdmin class in this story. We aimed to extend the Blog model by adding some properties and to implement the CK Editor in our app in this chapter. You can get chapter4’s code from below.

Photo by Faisal on Unsplash

Extending the Blog Model

We will add a property to the model. So, we will be getting the time when the Blog object was created.

from django.utils import timezoneclass Blog(models.Model):   @property
def get_when_was_created(self):
return ( - self.added_date).days

This property will return us the number of days since the object was created. Remember that we need to import timezonemodule for calculating this.

Image by Author

Extending the ModelAdmin Class


We can use the model properties in this field. If we add the property we have created earlier in list_display field of the ModelAdmin class, we can see it on the list table.

class BlogAdmin(admin.ModelAdmin):
list_display = ['title', 'added_date', 'updated_date',
'get_when_was_created' # the property we created
Image by Author

Also, we can do the same thing in the ModelAdmin class by using a little bit different way. I am going to add a method to the admin class.

from django.utils import timezoneclass BlogAdmin(admin.ModelAdmin):
def get_when_was_created_from_admin(self, obj):
return ( - obj.added_date).days

get_when_was_created_from_admin.short_description = 'How Many Days Ago?'

Actually, the code looks similar to what we have coded above. We added just obj parameter. Because, when the admin class calls this function, it will pass an object to it. Then, we can apply or calculate something by using the passed object to the function. Again I added the method’s name in list_display field. And as we expected, we see a new column: “How Many Days Ago?”.

Image by Author

There is a small difference, we can change the column name by using short_description property if we added the column by using the admin class. But we can not change the column name by using the model class property.

Adding Rich Text Editors: CK Editor

There are a bunch of editors to use. I will be using CK Editor.

Firstly, I’m going to install django-ckeditor via pip .

pip install django-ckeditor

Then add it to the INSTALLED_APPS.


Now, we need to implement it in the Blog model. I’m going to import RichTextField from ckeditor.fields in blog/ file. Then, change the content field.

from ckeditor.fields import RichTextFieldclass Blog(models.Model):
content = RichTextField()

And I complete this implementing process by migrating and pushing the changes to the database.

python makemigrations 
python migrate

We can see the implemented CK Editor by going to any blog object’s detail page.

Image by Author

Also, we can customize the editor. I am going to add a dictionary to file.

'default': {
'toolbar': 'full',
'height': 700,
'width': 1000

We can see the full toolbar with this option dictionary. If you want to more customize, you can check the documentation.

Image by Author


I enjoyed it story while writing and coding. Hopefully, you enjoyed it too while reading. If you liked the story, you can see more below.

Kind regards.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store