Django(1.11) models introduction
So a little overview of Django models and its’ instances. Django model is basically like a database table. To create one you need to change models.py and admin.py file. In models.py you declare your models and in admin.py you import the created models to generate tables in admin interface. To start using models you need to make “makemigrations” and “migrate” in console. If you haven’t created a superuser yet, you also need to do that to access Django admin panel.
Models.py:
Now I’ll go over different models instances I have used:
1) Foreign Key - ForeignKey can be a second model or a user
2) Int and char and text - number; short text; long text. Default is the value that is saved if you don’t change anything, can also be default = ‘’”
3) File upload:
4) Options: choose one
5) Checkbox options:
This is not a default field, needs to be installed in console: pip install django-multiselectfield
6) datefield:
I also have methods defined in my class.
admin.py file:
In console: (Windows) python manage.py makemigrations python manage.py migrate python manage.py createsuperuser username: e-mail: password: python manage.py runserver
In browser: http://127.0.0.1:8000/admin/ Log in and start adding data.