Model Definition Reference

  Django

Email

from django.db import models
from django.forms import ModelForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError

class Type(models.Model):

    id = models.AutoField(primary_key=True, db_column=u'id') 
    type = models.CharField(max_length=200)

    def __unicode__(self):
        return self.type     

class Category(models.Model):

    id = models.AutoField(primary_key=True, db_column=u'id') 
    category = models.CharField(max_length=200) 

    def __unicode__(self):
        return self.category

class Client(models.Model):

    id = models.AutoField(primary_key=True, db_column=u'id') 
    name = models.CharField(max_length=200,)
    ico = models.CharField(max_length=20,)
    vat = models.CharField(max_length=20, blank=True)
    # image = models.ImageField(upload_to="clients/images",height_field=300, width_field=300)

    address_street = models.CharField(max_length=100)
    address_zip = models.CharField(max_length=10)
    address_city = models.CharField(max_length=100)
    address_country = models.CharField(max_length=100)

    sale_max = models.SmallIntegerField(blank=True)
    sale_min = models.SmallIntegerField(blank=True)
    turnover = models.IntegerField(blank=True)

    contact_name = models.CharField(max_length=200)
    contact_email = models.EmailField()
    contact_website = models.URLField(blank=True)
    contact_phone = models.CharField(max_length=20) 
    contact_mobile = models.CharField(max_length=20,blank=True)
    contact_fax = models.CharField(max_length=20,blank=True)

    type = models.ForeignKey(Type)
    category = models.ForeignKey(Category)

    created_at = models.DateTimeField(auto_now_add=True) 
    modified_at = models.DateTimeField(auto_now=True) 
    created_by = models.ForeignKey(User)

    # note = models.TextField()
    # notes = models.ForeignKey(ClientNotes)
    # documets = models.ForeignKey(ClientDocs)
    # campaign = models.ForeignKey('production.campaign')   

    def __unicode__(self):
        return self.name

    def clean(self):
        # Max sale limit to 99% 
        if self.sale_max > 99:
            raise ValidationError('Max sale cannot be more than 99.')    	
        if (self.sale_max != None and self.sale_min != None):
            # Don't allow draft entries to have a pub_date.
            if self.sale_max == self.sale_min:
                raise ValidationError('Min/Max sale cannot be equal.')
            # Set the pub_date for published items if it hasn't been set already.
            if self.sale_max < self.sale_min:
                raise ValidationError('Max sale cannot be lower than min sale.')     
       
class Note(models.Model):

    id = models.AutoField(primary_key=True, db_column=u'id') 
    client = models.ForeignKey(Client)
    user = models.ForeignKey(User)
    title = models.CharField(max_length=50)
    note = models.TextField()
    date_created = models.DateTimeField(auto_now_add=True)  

    def __unicode__(self):
        return self.title

class Doc(models.Model):    

    id = models.AutoField(primary_key=True, db_column=u'id') 
    client = models.ForeignKey(Client)
    user = models.ForeignKey(User)
    title = models.CharField(max_length=50)
    document = models.FileField(upload_to='clients/docs/')
    date_created = models.DateTimeField(auto_now_add=True) 

    def __unicode__(self):
        return self.title

class HistoryLog(models.Model):

    id = models.AutoField(primary_key=True, db_column=u'id')
    user = models.ForeignKey(User)
    updated = models.DateTimeField(auto_now=True)   

    def __unicode__(self):
        return self.user
Spread the Code

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of