Hi folks,

For fun and as a way to test out and learn the idiosyncracies of the
various web frameworks out there in python land I've been writing a dvd
/ dvd player rental web application.

Now, I started it in Turbogears and used SQLObject to build the initial
(incomplete) model. I then decided to test Django because of its neat
admin interface... so converting the initial sqlobject model into the
Django ORM, here's what I came up so far:

<code>

from django.db.models import *
from django.db import models
from django.contrib.auth.models import User

# Choices for drop downs

DVD_RATINGS = [
    ('G', 'G'),
    ('PG', 'PG'),
    ('PG-13', 'PG-13'),
    ('R', 'R')
]

DVD_GENRE = [
    ('ac', 'action'),
    ('ho', 'horror'),
    ('dr', 'drama'),
    ('co', 'comedy'),
    ('ro', 'romance'),
    ('sc', 'scifi'),
    ('fa', 'fantasy'),
]

DVD_ORIG_LANGUAGE = [
    ('en', 'English'),
    ('it', 'Italian'),
    ('fr', 'French'),
    ('jp', 'Japanese'),
]

class ProductType(Model):
    name = CharField(maxlength=64)
    rental_price = FloatField(max_digits=6, decimal_places=2)
    def __str__(self):
        return self.name
    class Admin: pass

class Product(Model):
    name = CharField(maxlength=200)
    description = TextField()
    product_type = ForeignKey(ProductType)
    purchase_price = FloatField(max_digits=6, decimal_places=2)
    days_rented = IntegerField()
    def __str__(self):
        return str(self.name)
    class Admin: pass

class DvdPlayer(Product):
    brand = CharField(maxlength=200)
    def __str__(self):
        return self.name
    class Admin: pass

class Actor(Model):
    name = CharField(maxlength=64)
    def __str__(self):
        return self.name
    class Admin: pass

class Director(Model):
    name = CharField(maxlength=64)
    def __str__(self):
        return self.name
    class Admin: pass

class Dvd(Product):
    year = IntegerField()
    genre = CharField(maxlength=2, choices=DVD_GENRE)
    rating = CharField(maxlength=2, choices=DVD_RATINGS)
    language = CharField(maxlength=2, choices=DVD_ORIG_LANGUAGE)
    actors = ManyToManyField(Actor)
    director = ForeignKey(Director)
    length = IntegerField()
    discs = IntegerField(default=1)
    def __str__(self):
        return self.name
    class Admin:
        fields = (
            (None, {'fields': ('product_type', 'name',
'description')}),
            ('DVD Details', {
                'fields' : (
                    'genre', 'language', 'year', 'rating',
                    'director', 'actors',  'discs', 'length')
                }
            ),
            ('Historical', {'fields' : ( 'purchase_price',
'days_rented')})
        )
        list_display = ('product_type', 'name', 'director', 'genre',
'year', 'rating', 'language')
        list_filter = ('product_type', 'genre', 'director', 'language')
        search_fields = ('name',)


class Customer(Model):
    name = CharField(maxlength=200)
    email = EmailField()
    company = CharField(maxlength=200)
    room = IntegerField()
    def __str__(self):
        return self.name
    class Admin:
        list_display = ('name', 'company', 'room')
        search_fields = ('name', 'company', 'room')


class Order(Model):
    user = ForeignKey(User)
    customer = ForeignKey(Customer)
    order_date = DateTimeField(blank=True, null=True)
    entry_date = DateTimeField(auto_now_add=True)
    comment = TextField()
    #total = FloatField(max_digits=3, decimal_places=2, core=True)
    def __str__(self):
        return str(self.id) + '-' + str(self.order_date)
    class Admin:
        list_display = ('customer', 'order_date', 'user')
        list_filter = ('customer', 'order_date', 'user')

class OrderItem(Model):
    order = ForeignKey(Order, edit_inline=models.TABULAR,
num_in_admin=3)
    #~ estimated_delivery_date = DateTimeField()
    product = ForeignKey(Product, core=True)
    date_rented = DateTimeField(blank=True, null=True, core=True)
    date_returned = DateTimeField(blank=True, null=True, core=True)
    price = FloatField(max_digits=6, decimal_places=2, core=True)
    # price/cost should be a calculated property
    def __str__(self):
        return self.dvd.name
    class Admin:
        list_display = ('product', 'order', 'date_rented',
'date_returned', 'price')
        list_filter = ('product', 'date_rented', 'date_returned')
        search_fields = ('product',)

</code>

Obviously, this is a model in flux and I'm tweaking it as I go along,
it doesn't take into account inventory issues and probably other things
I haven't yet thought of... But I'm suspecting that I'm doing this
wrong somehow. For example, is this the right way to go about
inheritance in Django? By writing the model this way I seem to be
hardwiring the product into the admin crud interface when what I really
want to do is add either dvds or dvd players and not even display
product (which is abstract). Do I have to copy and redo the admin
interface to take this into consideration or is there a more generic
way of doing this?

Secondly, what's the best practice for writing tests to test the ORM
code and/or to populate the database with test data rather than having
to do this manually through the web interface.

Any help or suggestion would much appreciated. 

Alia


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to