{"action":"created","issue":{"url":"https://api.github.com/repos/ricaportela/api-abrolios/issues/1","repository_url":"https://api.github.com/repos/ricaportela/api-abrolios","labels_url":"https://api.github.com/repos/ricaportela/api-abrolios/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/ricaportela/api-abrolios/issues/1/comments","events_url":"https://api.github.com/repos/ricaportela/api-abrolios/issues/1/events","html_url":"https://github.com/ricaportela/api-abrolios/issues/1","id":190468567,"number":1,"title":"How to import CSV data with Django","user":{"login":"yelluw","id":23017027,"avatar_url":"https://avatars.githubusercontent.com/u/23017027?v=3","gravatar_id":"","url":"https://api.github.com/users/yelluw","html_url":"https://github.com/yelluw","followers_url":"https://api.github.com/users/yelluw/followers","following_url":"https://api.github.com/users/yelluw/following{/other_user}","gists_url":"https://api.github.com/users/yelluw/gists{/gist_id}","starred_url":"https://api.github.com/users/yelluw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yelluw/subscriptions","organizations_url":"https://api.github.com/users/yelluw/orgs","repos_url":"https://api.github.com/users/yelluw/repos","events_url":"https://api.github.com/users/yelluw/events{/privacy}","received_events_url":"https://api.github.com/users/yelluw/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-11-19T01:55:09Z","updated_at":"2016-11-19T01:57:37Z","closed_at":null,"body":"Django has something called a [command](https://docs.djangoproject.com/en/1.10/howto/custom-management-commands/). It allows you to use Django to run scripts with your models and data.\r\n\r\nThis is how it works:\r\n\r\n1. You define a command inside the `/app_name/management/commands` directory. Let's use `import_csv_data` as an example.\r\n\r\n2. There, you will write your code inside a simple class. To import data as CSV, python provides a very simple library.\r\n\r\n        import csv\r\n\r\n        def read_csv_file():\r\n            with open('filename.csv', 'r') as f:\r\n                return csv.DictReader(f)\r\n                # Note: csv.Dictreader imports the CSV data as a dictionary.\r\n\r\n        def store_data_in_db():\r\n            # first read the data\r\n            data = read_csv_file()\r\n            # then insert it into the databse model\r\n            for row in data:\r\n                the_object = Object.objects.create(\r\n                    foo=row.get('foo')\r\n                    )\r\n\r\n        # Then using the standard Django command syntax\r\n        # you use the method above to process the data.\r\n        # Include it in the handle() method\r\n\r\n        def handle(self, *args, **options):\r\n            self.store_data_in_db()\r\n            self.stdout.write(self.style.SUCCESS('Successfully imported the data!'))\r\n\r\nYou then run the command with the following syntax:\r\n\r\n`python manage.py command_name`\r\n\r\nIn this case it would be:\r\n\r\n`python manage.py import_csv_data`\r\n\r\nThat's it! :)"},"comment":{"url":"https://api.github.com/repos/ricaportela/api-abrolios/issues/comments/261685188","html_url":"https://github.com/ricaportela/api-abrolios/issues/1#issuecomment-261685188","issue_url":"https://api.github.com/repos/ricaportela/api-abrolios/issues/1","id":261685188,"user":{"login":"yelluw","id":23017027,"avatar_url":"https://avatars.githubusercontent.com/u/23017027?v=3","gravatar_id":"","url":"https://api.github.com/users/yelluw","html_url":"https://github.com/yelluw","followers_url":"https://api.github.com/users/yelluw/followers","following_url":"https://api.github.com/users/yelluw/following{/other_user}","gists_url":"https://api.github.com/users/yelluw/gists{/gist_id}","starred_url":"https://api.github.com/users/yelluw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yelluw/subscriptions","organizations_url":"https://api.github.com/users/yelluw/orgs","repos_url":"https://api.github.com/users/yelluw/repos","events_url":"https://api.github.com/users/yelluw/events{/privacy}","received_events_url":"https://api.github.com/users/yelluw/received_events","type":"User","site_admin":false},"created_at":"2016-11-19T01:57:37Z","updated_at":"2016-11-19T01:57:37Z","body":"@ricaportela "}}
{"actor":{"display_login":"yelluw"}}