{"id":7141,"date":"2013-01-28T07:00:07","date_gmt":"2013-01-28T07:00:07","guid":{"rendered":"http:\/\/vskills.in\/certification\/tutorial\/?p=7141"},"modified":"2024-04-12T14:17:09","modified_gmt":"2024-04-12T08:47:09","slug":"interacting-with-a-database-models","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/","title":{"rendered":"Interacting with a Database: Models"},"content":{"rendered":"<p><a class=\"vsc\" href=\"http:\/\/www.vskills.in\/certification\/Certified-Django Developer\"><span class=\"vsc-cn\" style=\"text-align: center;\"><span style=\"color: red;\">Certify and Increase Opportunity.<\/span><br \/>\n<span style=\"color: green;\">Be <\/span><br \/>\nGovt. Certified Django Developer<br \/>\n<\/span><\/a><\/p>\n<p>Interacting with a Database: Models<\/p>\n<h1>Django Integration<\/h1>\n<p>New in version 0.9.2.<\/p>\n<p>These functions streamline the process of initializing Django\u2019s settings module environment variable. Once this is done, your fabfile may import from your Django project, or Django itself, without requiring the use of&nbsp;<tt>manage.py<\/tt>&nbsp;plugins or having to set the environment variable yourself every time you use your fabfile.<\/p>\n<p>Currently, these functions only allow Fabric to interact with local-to-your-fabfile Django installations. This is not as limiting as it sounds; for example, you can use Fabric as a remote \u201cbuild\u201d tool as well as using it locally. Imagine the following fabfile:<\/p>\n<div>\n<div>\n<pre>from fabric.api import run, local, hosts, cd\nfrom fabric.contrib import django\n\ndjango.project('myproject')\nfrom myproject.myapp.models import MyModel\n\ndef print_instances():\n    for instance in MyModel.objects.all():\n        print(instance)\n\n@hosts('production-server')\ndef print_production_instances():\n    with cd('\/path\/to\/myproject'):\n        run('fab print_instances')<\/pre>\n<\/div>\n<\/div>\n<p>With Fabric installed on both ends, you could execute&nbsp;<tt>print_production_instances<\/tt>&nbsp;locally, which would trigger&nbsp;<tt>print_instances<\/tt>&nbsp;on the production server \u2013 which would then be interacting with your production Django database.<\/p>\n<p>As another example, if your local and remote settings are similar, you can use it to obtain e.g. your database settings, and then use those when executing a remote (non-Fabric) command. This would allow you some degree of freedom even if Fabric is only installed locally:<\/p>\n<div>\n<div>\n<pre>from fabric.api import run\nfrom fabric.contrib import django\n\ndjango.settings_module('myproject.settings')\nfrom django.conf import settings\n\ndef dump_production_database():\n    run('mysqldump -u %s -p=%s %s &gt; \/tmp\/prod-db.sql' % (\n        settings.DATABASE_USER,\n        settings.DATABASE_PASSWORD,\n        settings.DATABASE_NAME\n    ))<\/pre>\n<\/div>\n<\/div>\n<p>The above snippet will work if run from a local, development environment, again provided your local<tt>settings.py<\/tt>&nbsp;mirrors your remote one in terms of database connection info.<\/p>\n<dl>\n<dt id=\"fabric.contrib.django.project\"><tt>fabric.contrib.django.<\/tt><tt>project<\/tt><big>(<\/big><em>name<\/em><big>)<\/big><\/dt>\n<dd>Sets&nbsp;<tt>DJANGO_SETTINGS_MODULE<\/tt>&nbsp;to&nbsp;<tt>'&lt;name&gt;.settings'<\/tt>.This function provides a handy shortcut for the common case where one is using the Django default naming convention for their settings file and location.<\/p>\n<p>Uses&nbsp;<tt>settings_module<\/tt>&nbsp;\u2013 see its documentation for details on why and how to use this functionality.<\/dd>\n<\/dl>\n<dl>\n<dt id=\"fabric.contrib.django.settings_module\"><tt>fabric.contrib.django.<\/tt><tt>settings_module<\/tt><big>(<\/big><em>module<\/em><big>)<\/big><\/dt>\n<dd>Set&nbsp;<tt>DJANGO_SETTINGS_MODULE<\/tt>&nbsp;shell environment variable to&nbsp;<tt>module<\/tt>.Due to how Django works, imports from Django or a Django project will fail unless the shell environment variable&nbsp;<tt>DJANGO_SETTINGS_MODULE<\/tt>&nbsp;is correctly set (see&nbsp;the Django settings docs.)<\/p>\n<p>This function provides a shortcut for doing so; call it near the top of your fabfile or Fabric-using code, after which point any Django imports should work correctly.<\/p>\n<h1>Models<\/h1>\n<p>model is the single, definitive source of data about your data. It contains the essential fields and behaviors of the data you\u2019re storing. Generally, each model maps to a single database table.<\/p>\n<p>The basics:<\/p>\n<ul>\n<li>Each model is a Python class that subclasses&nbsp;<tt>django.db.models.Model<\/tt>.<\/li>\n<li>Each attribute of the model represents a database field.<\/li>\n<li>With all of this, Django gives you an automatically-generated database-access API; see&nbsp;Making queries.<\/li>\n<\/ul>\n<div id=\"s-quick-example\">\n<h2>Quick example<\/h2>\n<p>This example model defines a&nbsp;<tt>Person<\/tt>, which has a&nbsp;<tt>first_name<\/tt>&nbsp;and&nbsp;<tt>last_name<\/tt>:<\/p>\n<div>\n<div>\n<pre>from django.db import models\n\nclass Person(models.Model):\n    first_name = models.CharField(max_length=30)\n    last_name = models.CharField(max_length=30)<\/pre>\n<\/div>\n<\/div>\n<p><tt>first_name<\/tt>&nbsp;and&nbsp;<tt>last_name<\/tt>&nbsp;are&nbsp;fields&nbsp;of the model. Each field is specified as a class attribute, and each attribute maps to a database column.<\/p>\n<p>The above&nbsp;<tt>Person<\/tt>&nbsp;model would create a database table like this:<\/p>\n<div>\n<div>\n<pre>CREATE TABLE myapp_person (\n    \"id\" serial NOT NULL PRIMARY KEY,\n    \"first_name\" varchar(30) NOT NULL,\n    \"last_name\" varchar(30) NOT NULL\n);<\/pre>\n<div class=\"apply\">\n<h3>Apply for Django Certification Now!!<\/h3>\n<p><a href=\"http:\/\/www.vskills.in\/certification\/Certified-Django-Developer\">http:\/\/www.vskills.in\/certification\/Certified-Django-Developer<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Certify and Increase Opportunity. Be Govt. Certified Django Developer Interacting with a Database: Models Django Integration New in version 0.9.2. These functions streamline the process of initializing Django\u2019s settings module environment variable. Once this is done, your fabfile may import from your Django project, or Django itself, without requiring the use of&nbsp;manage.py&nbsp;plugins or having to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[283],"tags":[6793,6792,2590,5057,5058,2607],"class_list":["post-7141","page","type-page","status-publish","hentry","category-django","tag-django","tag-django-developer","tag-django-developer-certification","tag-django-integration","tag-django-models","tag-interacting-with-a-database-models"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Interacting with a Database: Models - Tutorial<\/title>\n<meta name=\"description\" content=\"Interacting with a Database: Models. Govt of India Certification for Django developer. Get Certified and improve employability. Certification assesses candidates in Django concepts.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Interacting with a Database: Models - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Interacting with a Database: Models. Govt of India Certification for Django developer. Get Certified and improve employability. Certification assesses candidates in Django concepts.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:09+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/\",\"name\":\"Interacting with a Database: Models - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2013-01-28T07:00:07+00:00\",\"dateModified\":\"2024-04-12T08:47:09+00:00\",\"description\":\"Interacting with a Database: Models. Govt of India Certification for Django developer. Get Certified and improve employability. Certification assesses candidates in Django concepts.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Interacting with a Database: Models\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Interacting with a Database: Models - Tutorial","description":"Interacting with a Database: Models. Govt of India Certification for Django developer. Get Certified and improve employability. Certification assesses candidates in Django concepts.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/","og_locale":"en_US","og_type":"article","og_title":"Interacting with a Database: Models - Tutorial","og_description":"Interacting with a Database: Models. Govt of India Certification for Django developer. Get Certified and improve employability. Certification assesses candidates in Django concepts.","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:09+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/","name":"Interacting with a Database: Models - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2013-01-28T07:00:07+00:00","dateModified":"2024-04-12T08:47:09+00:00","description":"Interacting with a Database: Models. Govt of India Certification for Django developer. Get Certified and improve employability. Certification assesses candidates in Django concepts.","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/interacting-with-a-database-models\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Interacting with a Database: Models"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/7141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=7141"}],"version-history":[{"count":5,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/7141\/revisions"}],"predecessor-version":[{"id":121615,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/7141\/revisions\/121615"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=7141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=7141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=7141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}