Django Models – Creating Customs Methods

This is our twenty first tutorial in Django, in this tutorial we are going to learn about Django Models Creating Customs Methods. before we have saw that how you can use built in methods in Django Models, for example in your Django Models when you use def __str__ ,  this is a built in method. with Django Models you can add your own custom methods. for example i have an Article model and i want to shorten the description text, so for this we are using our own methods.

 

 

 

Flask Web Development Tutorials

1: Flask CRUD Application with SQLAlchemy 

2: Flask Creating News Web Application 

3: Flask Creating REST API with Marshmallow 

 

 

 

Python GUI Development Tutorials

1: PyQt5 GUI Development Tutorials

2: Pyside2 GUI Development Tutorials

3: wxPython GUI Development Tutorials

4: Kivy GUI Development Tutorials 

5: TKinter GUI Development Tutorials

 

 

 

So now we need to create a New Project in Django, also for django installation and creating New Project you can read this article Django Introduction & Installation. but you can use this command for creating of the New Project.

 

 

 

 

After creating of the Django Project, you need to migrate your project.

make sure that you have changed the directory to the created project.

 

 

 

So after doing this, let’s create our Django App, i have already talked about

Django App in one of my articles, you can check this Django Apps & URL Routing.

 

 

 

 

After creation of the App, you need to add the created App in your settings.py file

INSTALLED_APP section.

 

 

 

 

Now open your models.py in your MyApp App, and add your model, basically we have

an Article model with three fields, and the important point is that we have added our

own custom method ShortenText() in our model.

in the method we are going to just return the 100 character of our body field text.

 

 

 

 

After creating of the models, we need to migrate our project, first you need to do

migrations and after that migrate.

 

 

 

 

Now you need to migrate.

 

 

 

 

OK now let’s just add our model to the Django admin panel, you can read this article for django super user   Django Creating Super User. open your admin.py file and add this code.

 

 

 

 

Before adding our html files, we need to create our views, so open your views.py file and add this code for creating a simple view.  basically in this view we are going to get our all articles from the database.

 

 

 

 

Now we are going to add our html files, make a templates folder in your working directory.

make sure that after creating of the templates, you need to add that in your settings.py file.

 

 

 

 

This is our base.html file, for django template inheritance you can read

this article Django Template Inheritance.

 

 

 

 

And this is our index.html file.

 

You can see that for the body we have used our {{articles.ShortenText}}.

 

 

 

 

Also you need to create your urls, you can read this article for url routing Django Apps & URL Routing . but just create a new python file in your MyApp app at name of urls.py and add these codes.

 

 

 

 

And also you need to include your app urls.py in your project urls.py file.

 

 

 

 

Now you can run your Django Project.

 

 

 

 

First you need to open your Django Admin Panel and add some data. i have already added some data like this. you can see that for the body text, i have added a long text, because i want to make this shorter using my custom method that i have already created in my models.py file.

Django Model Custom Method
Django Model Custom Method

 

 

 

 

 

Now if you go to  http://localhost:8000/ , you can see that we have just 100

character of body text.

Django Models - Creating Customs Methods
Django Models – Creating Customs Methods

 

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×