{"id":703,"date":"2019-01-23T23:21:15","date_gmt":"2019-01-23T14:21:15","guid":{"rendered":"https:\/\/www.nblog09.com\/w\/?p=703"},"modified":"2021-11-11T17:33:02","modified_gmt":"2021-11-11T08:33:02","slug":"django-model-migrate","status":"publish","type":"post","link":"https:\/\/www.nblog09.com\/w\/2019\/01\/23\/django-model-migrate\/","title":{"rendered":"django\u306emodels.py\u306e\u8a18\u8ff0\u3001\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3001\u7ba1\u7406\u753b\u9762\u3067\u306e\u64cd\u4f5c"},"content":{"rendered":"<h2 class=\"title-border\"><strong>models.py\u306e\u5b9a\u7fa9\u65b9\u6cd5<\/strong><\/h2>\n<p>\u30e2\u30c7\u30eb\u3068\u306f\u3001DB\u306e\u5404\u30c6\u30fc\u30d6\u30eb\u3068\u306e\u30a2\u30af\u30bb\u30b9\u3092\u3059\u308b\u969b\u306b\u7528\u3044\u3089\u308c\u308b\u30af\u30e9\u30b9\u3067\u3001djanog\u3067\u306fmodels.py\u306b\u30e2\u30c7\u30eb\u306e\u5b9a\u7fa9\u3092\u8a18\u8f09\u3059\u308b\u3002<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>models.py\u3067\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.db import models<\/p>\n<p>class Topic(models.Model): <strong>#Topic\u30e2\u30c7\u30eb<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;top_name = models.CharField(max_length=264, unique=True)<strong> #top_name\u30d7\u30ed\u30d1\u30c6\u30a3\u306f\u6700\u5927264\u6587\u5b57\u3067\u30e6\u30cb\u30fc\u30af\u306e\u6587\u5b57\u5217<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __str__(self):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.top_name<\/p>\n<p>class Webpage(models.Model): <strong># Webpage\u30e2\u30c7\u30eb<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;topic = models.ForeignKey(Topic, on_delete=models.CASCADE) <strong># Topic\u30e2\u30c7\u30eb\u3078\u306e\u5916\u90e8\u30ad\u30fc\u3001CASCADE\u306b\u3057\u3066\u3044\u308b\u305f\u3081\u3001Topic\u306e\u53c2\u7167\u5148\u304c\u524a\u9664\u3055\u308c\u308b\u3068\u53c2\u7167\u3055\u308c\u3066\u3044\u308b\u30ec\u30b3\u30fc\u30c9\u304c\u5f37\u5236\u7684\u306b\u524a\u9664\u3055\u308c\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;name = models.CharField(max_length=264, unique=True) <strong># name\u30d7\u30ed\u30d1\u30c6\u30a3\u306f\u6700\u5927264\u6587\u5b57\u3067\u30e6\u30cb\u30fc\u30af\u306e\u6587\u5b57\u5217<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;url = models.URLField(unique=True) <strong># url\u30d7\u30ed\u30d1\u30c6\u30a3\u306fURL\u306e\u6587\u5b57\u5217<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __str__(self):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.name<\/p>\n<p>class AccessRecord(models.Model): <strong># AccessRecord\u30e2\u30c7\u30eb<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;webpage= models.ForeignKey(Webpage, on_delete=models.CASCADE) <strong># Webpage\u30e2\u30c7\u30eb\u3078\u306e\u5916\u90e8\u30ad\u30fc<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;date = models.DateField() <strong># date\u306fDate\u578b<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __str__(self):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return str(self.date)\n<\/p><\/div>\n<\/div>\n<h2 class=\"title-border\"><strong>python manage.py migrate,python manage.py makemigrations\u306e\u5b9f\u884c\u306b\u3088\u308b\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3<\/strong><\/h2>\n<p>models.py\u3092\u8a2d\u5b9a\u3057\u305f\u3089<\/p>\n<ol>\n<li>python manage.py migrate<\/span><\/li>\n<li>python manage.py makemigrations \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u540d<\/li>\n<li>python manage.py migrate<\/li>\n<\/ol>\n<p><span class=\"pink_span\">\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u540d\u306b\u306fmodels.py\u3092\u66f4\u65b0\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u540d\u3092\u5165\u308c\u308b<\/span><\/p>\n<p>\u3092\u9806\u306b\u5b9f\u884c\u3059\u308b\u3002<\/p>\n<p>(2\u56de\u76ee\u4ee5\u964d\u306f\u3001<span class=\"pink_span\">python manage.py makemigrations \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u540d\u3001python manage.py migrate\u3060\u3051\u3067\u3088\u3044<\/span>)<br \/>\n1. python manage.py migrate\u3067settings.py\u306e\u8a2d\u5b9a\u306b\u5f93\u3063\u3066\u306e<span class=\"pink_span\">DB\u4f5c\u6210\u3001\u521d\u671f\u306e\u30c6\u30fc\u30d6\u30eb\u3092DB\u5185\u306b\u4f5c\u6210\u3059\u308b\u3002<\/span><br \/>\n2. python manage.py makemigrations models.py\u3092\u66f4\u65b0\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u540d\u3067\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306emodels.py\u306e\u8a18\u8f09\u306b\u6cbf\u3063\u305f\u30e2\u30c7\u30eb\u3092<span class=\"pink_span\">DB\u306b\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u51fa\u529b\u3055\u308c\u308b\u3002<\/span><\/p>\n<p>3. python migrate\u3092\u884c\u3046\u3053\u3068\u3067\u3001<span class=\"pink_span\">\u51fa\u529b\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u304cDB\u306b\u53cd\u6620\u3055\u308c\u3066\u30c6\u30fc\u30d6\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u3002<\/span><\/p>\n<h2 class=\"title-border\"><strong>\u7ba1\u7406\u753b\u9762\u3067\u30e2\u30c7\u30eb\u306e\u5229\u7528<\/strong><\/h2>\n<p>\u4f5c\u6210\u3057\u305f\u30e2\u30c7\u30eb\u3092\u7ba1\u7406\u753b\u9762\u3067\u633f\u5165\u3001\u66f4\u65b0\u3001\u53c2\u7167\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u306b\u306f\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306eadmin.py\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8f09\u3059\u308b\u3002<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>admin.py\u3078\u306e\u8a18\u8ff0<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.contrib import admin<br \/>\nfrom firstapp.models import AccessRecord,Topic,Webpage <strong># \u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30dd\u30fc\u30c8<\/strong><\/p>\n<p><span class=\"codehighlight\">admin.site.register<\/span>(AccessRecord)<br \/>\n<span class=\"codehighlight\">admin.site.register<\/span>(Topic)<br \/>\n<span class=\"codehighlight\">admin.site.register<\/span>(Webpage) <strong># \u7ba1\u7406\u753b\u9762\u306b\u3001\u30e2\u30c7\u30ebAccessRecord,Topic,Webpage\u3092\u8ffd\u52a0<\/strong>\n<\/div>\n<\/div>\n<p>\u6b21\u306b\u7ba1\u7406\u7528\u306e\u30b9\u30fc\u30d1\u30fc\u30e6\u30fc\u30b6\u30fc\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<p><span class=\"pink_span\">python manage.py createsuperuser<\/span><\/p>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3001http:\/\/{url}\/admin\u3067\u7ba1\u7406\u753b\u9762\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3002<\/p>\n<p>1.\u30ed\u30b0\u30a4\u30f3\u753b\u9762<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-706 aligncenter\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/login-300x196.png\" alt=\"\" width=\"418\" height=\"273\" srcset=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/login-300x196.png 300w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/login-768x503.png 768w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/login-1024x670.png 1024w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/login.png 1338w\" sizes=\"auto, (max-width: 418px) 100vw, 418px\" \/><\/p>\n<p>2.\u521d\u671f\u753b\u9762<\/p>\n<ul>\n<li>\u4e0a\u306eAUTHENTICATION\u3067\u306f\u3001\u7ba1\u7406\u753b\u9762\u5229\u7528\u30e6\u30fc\u30b6\u306e\u4f5c\u6210<\/li>\n<li>\u4e0b\u306f\u305d\u308c\u305e\u308c\u30a2\u30d7\u30ea\u306e\u30c6\u30fc\u30d6\u30eb\u3078\u30c7\u30fc\u30bf\u3092\u633f\u5165\u30fb\u524a\u9664\u30fb\u5909\u66f4\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-707 aligncenter\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/first_gamen-300x196.png\" alt=\"\" width=\"381\" height=\"249\" srcset=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/first_gamen-300x196.png 300w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/first_gamen-768x501.png 768w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/first_gamen-1024x668.png 1024w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/first_gamen.png 1342w\" sizes=\"auto, (max-width: 381px) 100vw, 381px\" \/><\/p>\n<p>\u4ee5\u4e0a\u3001\u30e2\u30c7\u30eb\u306e\u4f5c\u6210\u3068\u7ba1\u7406\u753b\u9762\u3067\u306e\u30e2\u30c7\u30eb\u306e\u64cd\u4f5c\u3067\u3057\u305f\u3002<\/p>\n<div class=\"django-adsense\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>models.py\u306e\u5b9a\u7fa9\u65b9\u6cd5 \u30e2\u30c7\u30eb\u3068\u306f\u3001DB\u306e\u5404\u30c6\u30fc\u30d6\u30eb\u3068\u306e\u30a2\u30af\u30bb\u30b9\u3092\u3059\u308b\u969b\u306b\u7528\u3044\u3089\u308c\u308b\u30af\u30e9\u30b9\u3067\u3001djanog\u3067\u306fmodels.py\u306b\u30e2\u30c7\u30eb\u306e\u5b9a\u7fa9\u3092\u8a18\u8f09\u3059\u308b\u3002 models.py\u3067\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b from djang [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":354,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[121,219],"tags":[132,122,133,14],"class_list":{"0":"post-703","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-django","8":"category-it-programming","9":"tag-admin","10":"tag-django","11":"tag-models","12":"tag-python","13":"entry"},"uagb_featured_image_src":{"full":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",709,480,false],"thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480-150x150.png",150,150,true],"medium":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480-300x203.png",300,203,true],"medium_large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",709,480,false],"large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",709,480,false],"1536x1536":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",709,480,false],"2048x2048":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",709,480,false],"home-middle":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",517,350,false],"home-top":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",620,420,false],"sidebar-thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",100,68,false]},"uagb_author_info":{"display_name":"9nmatsumoto","author_link":"https:\/\/www.nblog09.com\/w\/author\/9nmatsumoto\/"},"uagb_comment_info":0,"uagb_excerpt":"models.py\u306e\u5b9a\u7fa9\u65b9\u6cd5 \u30e2\u30c7\u30eb\u3068\u306f\u3001DB\u306e\u5404\u30c6\u30fc\u30d6\u30eb\u3068\u306e\u30a2\u30af\u30bb\u30b9\u3092\u3059\u308b\u969b\u306b\u7528\u3044\u3089\u308c\u308b\u30af\u30e9\u30b9\u3067\u3001djano&hellip;","_links":{"self":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/comments?post=703"}],"version-history":[{"count":19,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/703\/revisions"}],"predecessor-version":[{"id":3784,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/703\/revisions\/3784"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media\/354"}],"wp:attachment":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media?parent=703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/categories?post=703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/tags?post=703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}