How to use models on Django

Uciha Madara
3 min readOct 29, 2019

--

what is a model ?
So this model is directly related to the database, so if in django, this model will make the database to sql using the django or orm queryset.

Well later the controller or the view in django will call this model if you want to retrieve or enter something into the database.

Now we try to practice it, we use sqlite first which makes it easier.
So this sqlite is a database that is not server-based unlike mysql, mongo and others.

create your new app and give the name app userapp.

python3 manage.py startapp userapp

add userapp in settings.py so that the app is detected by django, in the Installed_Apps section.

#settings.pyINSTALLED_APPS = [    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'userapp']

create a model in userapp / models.py

#models.pyclass UserModel(models.Model):      nama = models.CharField(max_length=200)      email = models.CharField(max_length=200)      alamat = models.TextField()      email = models.EmailField()      username = models.CharField(max_length=200)      password = models.CharField(max_length=200)      def __str__(self):          return "{}".format(self.username)

then do makemigrations so that the model makes a query to send sqlite.

python3 manage.py makemigrations

then migrate it to send it to sqlite.

python3 manage.py migrate

first register our model to django admin, edit userapp / admin.py.

#admin.pyfrom django.contrib import adminfrom .models import UserModel# Register your models here.admin.site.register(UserModel)

now create a super user to access django admin.

python3 manage.py createsuperuser

running again server and open in browser.

http://127.0.0.1:8080/admin/

login admin

login admin django

dashboard admin

dashboard admin django

create new user

list user

--

--