Django Tutorials: Importing Models

By: Dusty Arlia
Published on Tuesday, April 22, 2014, 06:28 PM
Last Updated on Saturday, July 11, 2015 at 4:22 PM
Total Updates: 2

You can import a Django model class from its location on the Python path. Here is an example:

Your model:

class Author(models.Model):
    name = models.CharField(max_length=100)
    bio = models.TextField()

    def __unicode__(self):
        return self.name

Commands from your Python shell:

		
>>> from articles.models import Author
>>> b = Author(name='Dusty Arlia', bio='29 year old front-end developer from Cleveland, OH.')
>>> b.save()

These commands perform an INSERT SQL statement in the background. Django doesn’t hit the database until you explicitly call save(). The save() method has no return value.

Comments:

Ad:

Ad: