0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[django]データベースcreate!

Posted at

#Djangoでデータベースに保存

##model作成


from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField(default=0)

    def __str__(self):
        return '<Person:id=' + str(self.id) + ','+\
            self.name + '(' + str(self.age) + ')>'

モデルを作成したら、setting.pyのinstalled_apps = []のなかにアプリ名を追加
その後

python manage.py makemigrations "アプリ名"

その後


python manage.py migrate

これで使えるようになりました。

##shellでデータ保存

python manage.py shell
↓
from "アプリ名".models import Person
p = Person.objects.create(name="takuya",age="18")
p.save()

これで簡単なデータ保存ができます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?