How to work with models in django(Important)-1

let us take some Example ie., ....
 
 
 
from django.db import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Author(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField()

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Entry(models.Model):
    blog = models.ForeignKey(Blog)
    headline = models.CharField(max_length=255)
    body_text = models.TextField()
    pub_date = models.DateField()
    mod_date = models.DateField()
    authors = models.ManyToManyField(Author)
    n_comments = models.IntegerField()
    n_pingbacks = models.IntegerField()
    rating = models.IntegerField()

    def __str__(self):              # __unicode__ on Python 2
        return self.headline 
 
 
 
 
================
 
 
here we will see what acutally here we wrote
1. to create database we need to use class 


ie., 


Syntax: 
class classname(models.Model):
     column_name=models.datatype(attributes)
     def __str__(self):
         return self.column_name





2. so here the data type are 


CharField()
-----------
 
attribute will be maxlength 
 
 
ForeignKey(), ManyToManyField() 
--------------
 
attribute will be class name
 
 
ImageField
--------------- 
 
attribute will be upload_to="images/"
 
DateTimeField
---------------- 
auto_now_add =True  
 
 
Other
----- 
IntegerField()
TextField()
EmailField()
DateField() 

No comments:

Post a Comment