what is models.py in django code example
Example 1: create models in django
from django.db import models
class Contact(models.Model):
name = models.CharField(max_length=50)
email = models.CharField(max_length=50)
contact = models.CharField(max_length=50)
content = models.TextField()
def __str__(self):
return self.name + ' ' + self.email
Example 2: django models
Models in Django are classes that represent data base tables.
Example 3: django models syntax
from django.db import models
class Student(models.Model):
FRESHMAN = 'FR'
SOPHOMORE = 'SO'
JUNIOR = 'JR'
SENIOR = 'SR'
GRADUATE = 'GR'
YEAR_IN_SCHOOL_CHOICES = [
(FRESHMAN, 'Freshman'),
(SOPHOMORE, 'Sophomore'),
(JUNIOR, 'Junior'),
(SENIOR, 'Senior'),
(GRADUATE, 'Graduate'),
]
year_in_school = models.CharField(
max_length=2,
choices=YEAR_IN_SCHOOL_CHOICES,
default=FRESHMAN,
)
def is_upperclass(self):
return self.year_in_school in {self.JUNIOR, self.SENIOR}
Example 4: from django.db import models
from django.db import models
class Person(models.Model):
"""
This class creates a new table with
the name of your application and that class name.
The fully qualified name of the table
will be "appName_className".
Also, two fields with data type char will be
created: first_name and last_name.
"""
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)