Hello Django Users,
I am happy to announce the release of Django Check Constraints, a
Google Summer of Code Project. This project implements Value-based and
Range-based constraints in Django's models. The project will help in
server side validation at the database-level without writing extra
lines of code. Currently it support three databases Postgresql, Sqlite
and Oracle (these databases support check constraints natively).

Here is a sample model that uses Django Check Constraints:

class Product(models.Model):
        prod_name       =    models.CharField(maxlength=50)
        price                =    models.IntegerField()
        discount           =    models.IntegerField()
        tax_percent      =    models.IntegerField()
        sale_start_date =    models.DateField()
        sale_end_date  =    models.DateField()

        class Meta:
                constraints = (

                                ("check_price",Check(price__gte = 0) &
Check(price__gte = 'discount')),
                                ("check_tax",Check(tax_percent__gte =
10,tax_percent__lte = 20)),
 
("check_name",Check(name__not_in__upper = ("PRODA","PRODB","PRODC"))),
 
("check_discount_neq",Check(discount__neq = 0)),
 
("check_date",Check(sale_start_date__between =
[date(2007,01,01),date(2007,12,31)])),
 
("check_date_start",Check(sale_start_date__lte = 'sale_end_date')),

                              )


The equivalent SQL generated on syncdb is (in Postgresql):

BEGIN;
CREATE TABLE "test_app_product" (
    "id" serial NOT NULL PRIMARY KEY,
    "prod_name" varchar(50) NOT NULL,
    "price" integer NOT NULL,
    "discount" integer NOT NULL,
    "tax_percent" integer NOT NULL,
    "sale_start_date" date NOT NULL,
    "sale_end_date" date NOT NULL,
    CONSTRAINT "check_price" CHECK ( ("price" >= 0) AND ("price" >=
discount) ),
    CONSTRAINT "check_tax" CHECK ( ("tax_percent" <= 20) AND
("tax_percent" >= 10) ),
    CONSTRAINT "check_prod_name" CHECK (upper(prod_name) not in
('PRODA','PRODB','PRODC')),
    CONSTRAINT "check_discount_neq" CHECK ("discount" <> 0),
    CONSTRAINT "check_date" CHECK ("sale_start_date" between date
'2007-01-01' AND date '2007-12-31'),
    CONSTRAINT "check_date_start" CHECK ("sale_start_date" <=
sale_end_date)
)
;
COMMIT;

For more details you can check 
http://code.google.com/p/django-check-constraints/

On how to use it with newforms you can visit
http://thejuhyd.blogspot.com/2007/07/django-newforms-and-django-check.html
and
http://thejuhyd.blogspot.com/2007/08/django-newforms-and-django-check.html

Hoping to receive your feedback/suggestions/criticisms.

Cheers
Thejaswi Puthraya


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to