[PYTHON] Mobile Applicaiton development with IBM Bluemix Kinetise (2)

Continuing from Last time, I will explain the code modification of Python and the operation check with POSTMAN.

** 2. Python code fix ** 2.1. Modify kinetise \ settings.py. The following is an excerpt of the corrected part. Basically, it follows the description in Starter SDKs of Kinetise Help Center, but if you simply copy the contents of the website, single quotes will be full-width. Please note that it may cause an error.

setting.Excerpt from py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'comments',
    'rest_framework',
]
REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ],
        'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
        'rf_alter_api.renderers.AlterAPIRenderer'
    ],
    'DEFAULT_PARSER_CLASSES': [
        'rf_alter_api.parsers.AlterAPIParser',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [],
    'DEFAULT_PERMISSION_CLASSES': [],
        'EXCEPTION_HANDLER': 'rf_alter_api.views.alter_api_exception_handler',}

2.2. Run python manage.py createsuperuser at the command prompt of the virtual environment (env3). Below is the command log. Bluemix's Python runtime doesn't allow OS logins, so you can't run this command, and passwords can't be taken as arguments, so it's difficult to embed in a shell. Therefore, the admin user created for this task will be used as-is in Bluemix's Python runtime.

Command log


(env3) C:\Users\hoge\workspace_env3\kinetise>python manage.py createsuperuser --username any --email [email protected]
Password:
Password (again):
Superuser created successfully.

(env3) C:\Users\hoge\workspace_env3\kinetise>

2.3. Write a Model class called Comment in kinetise \ commnets \ models.py. The following is an excerpt of the description.

models.Excerpt from py


class Comment(models.Model):
    author = models.CharField('Author', max_length=64)
    message = models.CharField('Message', max_length=1024)
    pub_date = models.DateTimeField('Date published', auto_now_add=True)
    def __str__(self):
        return self.author + ": " + self.message

If you want to increase the JSON elements provided to the Mobile Application, add it to the Comment class. The return value is used for the label of the line on the admin screen, so it is essentially unnecessary. In the example below, an element called message2 is added.

models.Excerpt from py


class Comment(models.Model):
    author = models.CharField('Author', max_length=64)
    message = models.CharField('Message', max_length=1024)
    pub_date = models.DateTimeField('Date published', auto_now_add=True)
    message2 = models.CharField('Message2', max_length=1024)
    def __str__(self):
        return self.author + ": " + self.message + ": " + self.message2

2.4. Describe the association between the url and the view in kinetise \ urls.py. Follow the description of Starter SDKs in Kinetise Help Center, but if you add JSON element (message2) as in 2.3. Above, add it explicitly to fields. The following is an excerpt of the corrections.

urls.Excerpt from py


class CommentsSerializer(AlterAPISerializer):
    context = serializers.SerializerMethodField()
    class Meta:
        model = Comment
        fields = ('context', 'author', 'message', "pub_date", 'message2',)

2.5. Write comments \ admin.py as described in the Starter SDKs in the Kinetise Help Center.

2.6. Right-click on the kinetise project and run manage.py makemigrations comments and manage.py migrate. In addition, check how Comment is implemented in sqlite3 with the following command just in case. This may help you understand the error output if it doesn't work properly.

(env3) C:\Users\hoge\workspace_env3\kinetise>python manage.py sqlmigrate comments 0001
BEGIN;
--
-- Create model Comment
--
CREATE TABLE "comments_comment" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "author" varchar(64) NOT NULL, "message" varchar(1024) NOT NULL, "pub_date" datetime NOT NULL, "message2" varchar(1024) NOT NULL);
COMMIT;

(env3) C:\Users\hoge\workspace_env3\kinetise>

2.7. Run manage.py runserver --noreload from Eclipse. If you get an error, review the steps up to this point. Qiita post kinetise start.png

** 3. Confirm operation with POSTMAN ** 3.1. Use POSTMAN to verify GET and POST behavior. For POST, specify the URL as http://127.0.0.1:8000/comments/, specify raw and json in BODY and send, and if Status is 200 OK, it is working normally. .. qiita投稿POSTでPOSTMAN.png

For GET, specify the same URL as POST, and if Status is 200 OK, it is operating normally. qiita投稿GETでPOSTMAN.png (Supplement) If it does not work properly, there is no choice but to review the work so far, but in the browser `http://127.0.0.1:8000/admin/``` and http://127.0 You can also help by accessing .0.1: 8000 / comments / `` and checking the console output of Eclipse.

This concludes the explanation of Python code modification and operation check with POSTMAN. Next, I will explain Deploying Bluemix to Python runtime.

Recommended Posts

Mobile Applicaiton development with IBM Bluemix Kinetise (3)
Mobile Applicaiton development with IBM Bluemix Kinetise (2)
Mobile Applicaiton development with IBM Bluemix Kinetise (1)
Virtualize (isolate) IBM i python development environment with chroot
Development digest with Django
Bluemix Python Microservices Development