{"id":1980,"date":"2021-04-03T07:00:57","date_gmt":"2021-04-03T07:00:57","guid":{"rendered":"https:\/\/hthirukkumaran.com\/?p=1980"},"modified":"2021-04-03T07:00:59","modified_gmt":"2021-04-03T07:00:59","slug":"django-getting-started","status":"publish","type":"post","link":"https:\/\/hthirukkumaran.com\/index.php\/django-getting-started\/","title":{"rendered":"Django Getting Started"},"content":{"rendered":"\n<p>This is my reference notes<\/p>\n\n\n\n<p>python -m django &#8211;version<\/p>\n\n\n\n<p>django-admin startproject mysite<\/p>\n\n\n\n<p>python manage.py runserver<\/p>\n\n\n\n<p>Project<br>     Apps<br>         -Views &#8211; Controller &#8211; Model Validation on the server side<br>             -Templates &#8211; Views (Models Validation on the Client Side) &#8211; Javascript disabled &#8211; Submit<br>         -Settings.py add the following connection string as default.<br>             &#8216;default&#8217;: {<br>                         &#8216;ENGINE&#8217;: &#8216;django.db.backends.mysql&#8217;,<br>                         &#8216;NAME&#8217;: &#8216;northwind&#8217;,<br>                         &#8216;USER&#8217;: &#8216;developer&#8217;,<br>                         &#8216;PASSWORD&#8217;: &#8216;Kingdom&#8217;,<br>                         &#8216;HOST&#8217;: &#8216;127.0.0.1&#8217;,<br>                         &#8216;PORT&#8217;: &#8216;3306&#8217;,<br>                     },<br>         -Run this command to install the mysql client globally using pip.<br>             pip install mysqlclient<br>     Admin App<br>         -Register all models in the Admin app. <br>         -Master data entry should be done via App.<br>         -All master data tables must start with ref_<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Models\n    -Foreign key relationships must be maintained\n    -Migrations must be used so that db deployment is done in correct steps as per the dependency.\n    -Models additional validation must be done via validators https:\/\/docs.djangoproject.com\/en\/3.1\/ref\/validators\/<\/code><\/pre>\n\n\n\n<p>Deployment<br>\n    Production deployment must have a new secret key in the project&#8217;s settings.py<br>\n    Project&#8217;s url.py should have the url entries for all applications otherwise we will get 404.<\/p>\n\n\n\n<p>After the project is created run the following command and see the default homepage at this location.<br>\n    python manage.py runserver<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">    http:\/\/127.0.0.1:8000\/  <\/h2>\n\n\n\n<p>Step2 : Admin site setup and users management<\/p>\n\n\n\n<p>Before creating any apps migrate admin models to database using the following command.<br>\n    python manage.py migrate<br>\nCreate a superuser using the following command.<br>\n    python manage.py createsuperuser<br>\nRun the following query against the auth_user table to see if the superadmin is created successfully<br>\n    SELECT * FROM djangotutorial.auth_user;<br>\n<em><strong>NOTE: After deployment to production. Ask the client to  change password for the superadmin via admin app. <\/strong><\/em>**<br>\nLogin to admin portal and enter first name and last name of the super admin if you want.<br>\nCreate all the roles needed for your applications in the admin portal.<br>\nGrant required permission to all roles<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Give additional permissions to users in addition to roles permissions<\/h2>\n\n\n\n<p>App Creation and Models Creation and Data Entry for Ref tables.<\/p>\n\n\n\n<p>Create an app under this project using the following command.<br>\n    python manage.py startapp cruddemo<br>\nAdd urls.py to the app folder and initialize the app name as shown below<br>\n    from . import views<br>\n    from django.urls import path<br>\n    app_name = &#8216;cruddemo&#8217;<br>\nCreate a sample table ref_record_status in Mysql and run the following command to generate python model class.<br>\n    python manage.py inspectdb &gt; output.txt<br>\n    Copy only the models that your application needs from the output.txt file to models.py in your app.<br>\nRegister the model in the admin portal.<br>\n    in admin.py of the app add the following lines. Copy the model name from models.py file.<br>\n    from .models import RefRecordStatus<br>\n    admin.site.register(RefRecordStatus)<br>\nIn project settings.py add the following entry in INSTALLED_APPS<br>\n    &#8216;cruddemo.apps.CruddemoConfig&#8217;, <br>\nRun the following command and login to the admin portal.<br>\nAdd data to the reference tables via GUI.<br>\nTODO: Generate python code for migrating initial data also. **<\/p>\n\n\n\n<p>Step 4: Deploying the database objects from admin and app to new database.<\/p>\n\n\n\n<ol><li>Create a new database and update the name of the database in default connection string.<\/li><li>Run this command to migrate all the admin objects.<br>\npython manage.py migrate<\/li><li>To migrate the objects of the applications run the following command.<br>\npython manage.py makemigrations cruddemo<br>\npython manage.py sqlmigrate cruddemo 0001<br>\npython manage.py migrate<\/li><\/ol>\n\n\n\n<p>This did not work. **We need to solve it in the future.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is my reference notes python -m django &#8211;version django-admin startproject mysite python manage.py runserver Project Apps -Views &#8211; Controller &#8211; Model Validation on the server side -Templates &#8211; Views (Models Validation on the Client Side) &#8211; Javascript disabled &#8211; Submit -Settings.py add the following connection string as default. &#8216;default&#8217;: { &#8216;ENGINE&#8217;: &#8216;django.db.backends.mysql&#8217;, &#8216;NAME&#8217;: &#8216;northwind&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":""},"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/hthirukkumaran.com\/index.php\/wp-json\/wp\/v2\/posts\/1980"}],"collection":[{"href":"https:\/\/hthirukkumaran.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hthirukkumaran.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hthirukkumaran.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hthirukkumaran.com\/index.php\/wp-json\/wp\/v2\/comments?post=1980"}],"version-history":[{"count":1,"href":"https:\/\/hthirukkumaran.com\/index.php\/wp-json\/wp\/v2\/posts\/1980\/revisions"}],"predecessor-version":[{"id":1981,"href":"https:\/\/hthirukkumaran.com\/index.php\/wp-json\/wp\/v2\/posts\/1980\/revisions\/1981"}],"wp:attachment":[{"href":"https:\/\/hthirukkumaran.com\/index.php\/wp-json\/wp\/v2\/media?parent=1980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hthirukkumaran.com\/index.php\/wp-json\/wp\/v2\/categories?post=1980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hthirukkumaran.com\/index.php\/wp-json\/wp\/v2\/tags?post=1980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}