12(b). ok now we will work on models in django

let us create one app that is .... contact in the current project ie., BTMS



erp@erp-OptiPlex-330:~/btms$ django-admin startapp contacts
erp@erp-OptiPlex-330:~/btms$ tree
.
|-- admin
|   |-- css
|   |   |-- base.css
|   |   |-- changelists.css
|   |   |-- dashboard.css
|   |   |-- forms.css
|   |   |-- ie.css
|   |   |-- login.css
|   |   |-- rtl.css
|   |   `-- widgets.css
|   |-- img
|   |   |-- changelist-bg.gif
|   |   |-- changelist-bg_rtl.gif
|   |   |-- chooser-bg.gif
|   |   |-- chooser_stacked-bg.gif
|   |   |-- default-bg.gif
|   |   |-- default-bg-reverse.gif
|   |   |-- deleted-overlay.gif
|   |   |-- gis
|   |   |   |-- move_vertex_off.png
|   |   |   `-- move_vertex_on.png
|   |   |-- icon_addlink.gif
|   |   |-- icon_alert.gif
|   |   |-- icon_calendar.gif
|   |   |-- icon_changelink.gif
|   |   |-- icon_clock.gif
|   |   |-- icon_deletelink.gif
|   |   |-- icon_error.gif
|   |   |-- icon-no.gif
|   |   |-- icon_searchbox.png
|   |   |-- icon_success.gif
|   |   |-- icon-unknown.gif
|   |   |-- icon-yes.gif
|   |   |-- inline-delete-8bit.png
|   |   |-- inline-delete.png
|   |   |-- inline-restore-8bit.png
|   |   |-- inline-restore.png
|   |   |-- inline-splitter-bg.gif
|   |   |-- nav-bg.gif
|   |   |-- nav-bg-grabber.gif
|   |   |-- nav-bg-reverse.gif
|   |   |-- nav-bg-selected.gif
|   |   |-- selector-icons.gif
|   |   |-- selector-search.gif
|   |   |-- sorting-icons.gif
|   |   |-- tool-left.gif
|   |   |-- tool-left_over.gif
|   |   |-- tool-right.gif
|   |   |-- tool-right_over.gif
|   |   |-- tooltag-add.gif
|   |   |-- tooltag-add_over.gif
|   |   |-- tooltag-arrowright.gif
|   |   `-- tooltag-arrowright_over.gif
|   `-- js
|       |-- actions.js
|       |-- actions.min.js
|       |-- admin
|       |   |-- DateTimeShortcuts.js
|       |   `-- RelatedObjectLookups.js
|       |-- calendar.js
|       |-- collapse.js
|       |-- collapse.min.js
|       |-- core.js
|       |-- inlines.js
|       |-- inlines.min.js
|       |-- jquery.init.js
|       |-- jquery.js
|       |-- jquery.min.js
|       |-- LICENSE-JQUERY.txt
|       |-- prepopulate.js
|       |-- prepopulate.min.js
|       |-- SelectBox.js
|       |-- SelectFilter2.js
|       |-- timeparse.js
|       `-- urlify.js
|-- btms
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- settings.py
|   |-- settings.py~
|   |-- settings.pyc
|   |-- urls.py
|   |-- urls.py~
|   |-- urls.pyc
|   |-- wsgi.py
|   `-- wsgi.pyc
|-- contacts
|   |-- admin.py
|   |-- __init__.py
|   |-- models.py
|   |-- tests.py
|   `-- views.py

|-- db.sqlite3
|-- login
|   |-- admin.py
|   |-- admin.pyc
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- models.py
|   |-- models.pyc
|   |-- templates
|   |   |-- home.html
|   |   `-- home.html~
|   |-- tests.py
|   |-- views.py
|   |-- views.py~
|   `-- views.pyc
`-- manage.py

10 directories, 98 files





so in the contacts we will work on model.py


Django models map (roughly) to a database table, and provide a place to encapsulate business logic. All models subclass the base Model class, and contain field definitions. Let’s start by creating a simple Contact model for our application in contacts/models.py.
from django.db import models


class Contact(models.Model):

    first_name = models.CharField(
        max_length=255,
    )
    last_name = models.CharField(
        max_length=255,

    )

    email = models.EmailField()

    def __str__(self):

        return ' '.join([
            self.first_name,
            self.last_name,
        ])
Django provides a set of fields that map to data types and different validation rules. For example, the EmailField here maps to the same column type as the CharField, but adds validation for the data.
Once you’ve created a model, you need to update your database with the new tables. Django’s syncdb command looks for models that are installed and creates tables for them if needed.
(tutorial)$ python ./manage.py syncdb

Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site

...
Our contact table isn’t anywhere to be seen. The reason is that we need to tell the Project to use the Application.
The INSTALLED_APPS setting lists the applications that the project uses. These are listed as strings that map to Python packages. Django will import each and looks for a models module there. Add our Contacts app to the project’s INSTALLED_APPS setting:
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs', 
    'login',
    'contacts',
 
)
Then run syncdb again:
(tutorial)$ python ./manage.py syncdb
Creating tables ...
Creating table contacts_contact
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
Note that Django created a table named contacts_contact: by default Django will name your tables using a combination of the application name and model name. You can override that with the model Meta options.

 


erp@erp-OptiPlex-330:~/btms$ python manage.py syncdb
Creating tables ...
Creating table contacts_contact
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
 







Interacting with the Model
Now that the model has been synced to the database we can interact with it using the interactive shell.

(tutorial)$ python ./manage.py shell
Python 2.7.3 (default, Aug  9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from contacts.models import Contact
>>> Contact.objects.all()
[]
>>> Contact.objects.create(first_name='Nathan', last_name='Yergler')
<Contact: Nathan Yergler>
>>> Contact.objects.all()
[<Contact: Nathan Yergler>]
>>> nathan = Contact.objects.get(first_name='Nathan')
>>> nathan
<Contact: Nathan Yergler>
>>> print nathan
Nathan Yergler
>>> nathan.id
1



Sqlite3


erp@erp-OptiPlex-330:~/btms$ sqlite3 db.sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from contacts_contact;
1|Nathan|Yergler|
sqlite>













 






No comments:

Post a Comment