django:create a model …

 

hide@deb1:~/dj/cats$ vi jellicle/models.py

from django.db import models

# Create your models here.

class Cat(models.Model):
  name = models.CharField(maxlength=100)
  description= models.CharField(maxlength=100)
  createdt = models.DateTimeField()
  updatedt = models.DateTimeField()

hide@deb1:~/dj/cats$ python manage.py sql jellicle

BEGIN;
CREATE TABLE `jellicle_cat` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name` varchar(100) NOT NULL,
    `description` varchar(100) NOT NULL,
    `createdt` datetime NOT NULL,
    `updatedt` datetime NOT NULL
);
COMMIT;

hide@deb1:~/dj/cats$ echo "show tables" | mysql -h localhost -u cats cats -p
Enter password:
Tables_in_cats
auth_group
auth_group_permissions
auth_message
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_content_type
django_session
django_site

hide@deb1:~/dj/cats$ python manage.py syncdb
Creating table jellicle_cat
Adding permission ‘cat | Can add cat’
Adding permission ‘cat | Can change cat’
Adding permission ‘cat | Can delete cat’

hide@deb1:~/dj/cats$ echo "show tables" | mysql -h localhost -u cats cats -p
Enter password:
Tables_in_cats
auth_group
auth_group_permissions
auth_message
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_content_type
django_session
django_site
jellicle_cat

hide@deb1:~/dj/cats$ python manage.py shell
Python 2.4.4 (#2, Apr  5 2007, 20:11:18)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from cats.jellicle.models import Cat
>>> Cat.objects.all()
[]
>>> from datetime import datetime
>>> c=Cat(name='Old Deuteronomy',description='He is the wise old leader. A safe haven for any scared cat! His gentle hand with any of the kittens earns him a lot of respects',createdt=datetime.now(),updatedt=datetime.now())

 

hide@deb1:~/dj/cats$ echo "select * from jellicle_cat" | mysql -h localhost -u cats cats -p
Enter password:
hide@deb1:~/dj/cats$

 

>>> c.save()
/var/lib/python-support/python2.4/django/db/backends/mysql/base.py:35: Warning: Data truncated for column 'description' at row 1
  return self.cursor.execute(sql, params)

 

hide@deb1:~/dj/cats$ vi jellicle/models.py

      description= models.TextField()

hide@deb1:~/dj/cats$ python manage.py sql jellicle
BEGIN;
CREATE TABLE `jellicle_cat` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name` varchar(100) NOT NULL,
    `description` longtext NOT NULL,
    `createdt` datetime NOT NULL,
    `updatedt` datetime NOT NULL
);
COMMIT;

hide@deb1:~/dj/cats$ python manage.py reset jellicle

You have requested a database reset.
This will IRREVERSIBLY DESTROY any data in your database.
Are you sure you want to do this?

Type ‘yes’ to continue, or ‘no’ to cancel: yes

hide@deb1:~/dj/cats$ echo "desc jellicle_cat" | mysql -h localhost -u cats cats -p
Enter password:
Field   Type    Null    Key     Default Extra
id      int(11) NO      PRI     NULL    auto_increment
name    varchar(100)    NO
description     longtext        NO
createdt        datetime        NO
updatedt        datetime        NO

Python 2.4.4 (#2, Apr  5 2007, 20:11:18)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from cats.jellicle.models import Cats
Traceback (most recent call last):
  File "<console>", line 1, in ?
ImportError: cannot import name Cats
>>> from cats.jellicle.models import Cat
>>> from datetime import datetime
>>> c=Cat(name='Old Deuteronomy',description='He is the wise old leader. A safe haven for any scared cat! His gentle hand with any of the kittens earns him a lot of respects',createdt=datetime.now(),updatedt=datetime.now())
>>> c.save()

hide@deb1:~/dj/cats$ echo "select * from jellicle_cat" | mysql -h localhost -u cats cats -p
Enter password:
id      name    description     createdt        updatedt
1       Old Deuteronomy He is the wise old leader. A safe haven for any scared cat! His gentle hand with any of the kittens earns him a lot of respects   2008-04-09 15:49:27       2008-04-09 15:49:27

>>> c.id
1L
>>> c.name
'Old Deuteronomy'
>>> c.updatedt
datetime.datetime(2008, 4, 9, 15, 49, 27, 531160)

hide@deb1:~/dj/cats$ echo "select updatedt from jellicle_cat" | mysql -h localhost -u cats cats -p
Enter password:
updatedt
2008-04-09 15:49:27

>>> c.updatedt=datetime.now()
>>> c.save()
>>>

hide@deb1:~/dj/cats$ echo "select updatedt from jellicle_cat" | mysql -h localhost -u cats cats -p
Enter password:
updatedt
2008-04-09 15:51:59

>>> c.updatedt
datetime.datetime(2008, 4, 9, 15, 51, 59, 854354)
>>> c=Cat(name='Munkustrap',description='This handsome member of the tribe is said to be Old Deuteronomy''s son. He is also the shows narrator and a striking grey tabby that is hard to miss! ',createdt=datetime.now(),updatedt=datetime.now())
>>> c.save()
>>> c.id
2L

hide@deb1:~/dj/cats$ echo "select name from jellicle_cat" | mysql -h localhost -u cats cats -p
Enter password:
name
Old Deuteronomy
Munkustrap

>>> Cat.objects.all()
[<Cat: Cat object>, <Cat: Cat object>]

>>> for x in Cat.objects.all():
...   print x.id
...
1
2

>>> Cat.objects.filter(id=2)[0].name
'Munkustrap'

>>> Cat.objects.filter(name__startswith='Old')[0].name
'Old Deuteronomy'

>>> Cat.objects.filter(createdt__year=2008)
[<Cat: Cat object>, <Cat: Cat object>]

>>> Cat.objects.get(id=2)
<Cat: Cat object>
>>> Cat.objects.get(id=3)
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/var/lib/python-support/python2.4/django/db/models/manager.py", line 67, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/var/lib/python-support/python2.4/django/db/models/query.py", line 213, in get
    raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name
DoesNotExist: Cat matching query does not exist.

>>> Cat.objects.get(pk=1)
<Cat: Cat object>

カテゴリー: 未分類 パーマリンク

django:create a model … への5件のフィードバック

  1. Unknown より:

    wow gold!All wow gold US Server 24.99$/1000G on sell! Cheap wow gold,wow gold,wow gold,Buy Cheapest/Safe/Fast WoW US EU wow gold Power leveling wow gold from the time you World of Warcraft gold ordered! -13555751604345

  2. Unknown より:

    Hi,Do you need digital signages, advertising displays, digital sign, advertisement displays and advertising players? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.
    amberdigital Contact Us
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[cbbhcdiaeijfej]

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト / 変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト / 変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト / 変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト / 変更 )

%s と連携中