martes, 26 de noviembre de 2013

Trabajando con modelos

En el archivo models.py

from django.db import models

# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=200)
    body = models.TextField( )
    pub_date =  models.DateTimeField('date published')
    likes = models.IntegerField()

    def __unicode__(self):
    return self.title

Sincronizamos la base

(django-cesar)cesar@developerWEB:~/django-cesar/django_test$ python manage.py syncdb


Esto nos crea las tablas del proyecto en postgresql

ejemplo

(django-cesar)cesar@developerWEB:~/django-cesar/django_test$ python manage.py sql article
BEGIN;
CREATE TABLE "article_article" (
    "id" serial NOT NULL PRIMARY KEY,
    "title" varchar(200) NOT NULL,
    "body" text NOT NULL,
    "pub_date" timestamp with time zone NOT NULL,
    "likes" integer NOT NULL
)
;

COMMIT;

y desúes podemos  trabajar con el model desde el shell

(django-cesar)cesar@developerWEB:~/django-cesar/django_test$ python manage.py shell

>>> from article.models import Article
>>> Article.objetc.all()
>>> a = Article(title="test 2", body="bla", pub_date=timezone.now(), likes=0)
>>> a.save()
>>> a = Article(title="test 3", body="bla", pub_date=timezone.now(), likes=0)
>>> a.save()
>>> Article.objects.all()

>>> Article.objects.all()
[<Article: test 1>, <Article: test 2>, <Article: test 3>]
>>> Article.objects.all()
[<Article: test 1>, <Article: test 2>, <Article: test 3>]



No hay comentarios.:

Publicar un comentario