• Complain

it-ebooks - Mastering Django Core

Here you can read online it-ebooks - Mastering Django Core full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2017, publisher: iBooker it-ebooks, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

it-ebooks Mastering Django Core
  • Book:
    Mastering Django Core
  • Author:
  • Publisher:
    iBooker it-ebooks
  • Genre:
  • Year:
    2017
  • Rating:
    5 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 100
    • 1
    • 2
    • 3
    • 4
    • 5

Mastering Django Core: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Mastering Django Core" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

it-ebooks: author's other books


Who wrote Mastering Django Core? Find out the surname, the name of the author of the book and a list of all author's works by series.

Mastering Django Core — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Mastering Django Core" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Appendix A: Model Definition Reference

Chapter 4 explains the basics of defining models, and we use them throughout the rest of the book. There is, however, a huge range of model options available not covered elsewhere. This appendix explains each possible model definition option.

Note that although these APIs are considered stable, the Django developers consistently add new shortcuts and conveniences to the model definition. Its a good idea to always check the latest documentation online at http://docs.djangoproject.com/.

Fields

The most important part of a model and the only required part of a model is the list of database fields it defines.

Field Name Restrictions

Django places only two restrictions on model field names:

  1. A field name cannot be a Python reserved word, because that would result in a Python syntax error. For example:

    class Example(models.Model): pass = models.IntegerField() # 'pass' is a reserved word!
  2. A field name cannot contain more than one underscore in a row, due to the way Djangos query lookup syntax works. For example:

    class Example(models.Model): foo__bar = models.IntegerField() # 'foo__bar' has two underscores!

Each field in your model should be an instance of the appropriate Field class. Django uses the field class types to determine a few things:

  • The database column type (e.g., INTEGER, VARCHAR).
  • The widget to use in Djangos forms and admin site, if you care to use it (e.g., ,
Appendix B: Database API Reference

Djangos database API is the other half of the model API discussed in Appendix A. Once youve defined a model, youll use this API any time you need to access the database. Youve seen examples of this API in use throughout the book; this appendix explains all the various options in detail.

Like the model APIs discussed in Appendix A, though these APIs are considered very stable, the Django developers consistently add new shortcuts and conveniences. Its a good idea to always check the latest documentation online, available at http://docs.djangoproject.com/.

Throughout this guide (and in the reference), well refer to the following models, which comprise a Weblog application:

from django.db import modelsclass Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __str__(self): return self.nameclass Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() def __str__(self): return self.nameclass 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
Creating objects

To represent database-table data in Python objects, Django uses an intuitive system: A model class represents a database table, and an instance of that class represents a particular record in the database table.

To create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database.

Assuming models live in a file mysite/blog/models.py, heres an example:

>>> from blog.models import Blog>>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')>>> b.save()

This performs an INSERT SQL statement behind the scenes. Django doesnt hit the database until you explicitly call save().

The save() method has no return value.

See also

save() takes a number of advanced options not described here. See the documentation for save() for complete details.

To create and save an object in a single step, use the create() method.

Saving changes to objects

To save changes to an object thats already in the database, use save().

Given a Blog instance b5 that has already been saved to the database, this example changes its name and updates its record in the database:

>>> b5.name = 'New name'>>> b5.save()

This performs an UPDATE SQL statement behind the scenes. Django doesnt hit the database until you explicitly call save().

Saving ForeignKey and ManyToManyField fields

Updating a ForeignKey field works exactly the same way as saving a normal field simply assign an object of the right type to the field in question. This example updates the blog attribute of an Entry instance entry, assuming appropriate instances of Entry and Blog are already saved to the database (so we can retrieve them below):

>>> from blog.models import Entry>>> entry = Entry.objects.get(pk=1)>>> cheese_blog = Blog.objects.get(name="Cheddar Talk")>>> entry.blog = cheese_blog>>> entry.save()

Updating a ManyToManyField works a little differently use the add() method on the field to add a record to the relation. This example adds the Author instance joe to the entry object:

>>> from blog.models import Author>>> joe = Author.objects.create(name="Joe")>>> entry.authors.add(joe)

To add multiple records to a ManyToManyField in one go, include multiple arguments in the call to add(), like this:

>>> john = Author.objects.create(name="John")>>> paul = Author.objects.create(name="Paul")>>> george = Author.objects.create(name="George")>>> ringo = Author.objects.create(name="Ringo")>>> entry.authors.add(john, paul, george, ringo)

Django will complain if you try to assign or add an object of the wrong type.

Retrieving objects

To retrieve objects from your database, construct a QuerySet via a Manager on your model class.

A QuerySet represents a collection of objects from your database. It can have zero, one or many filters. Filters narrow down the query results based on the given parameters. In SQL terms, a QuerySet equates to aSELECT statement, and a filter is a limiting clause such as WHERE or LIMIT.

You get a QuerySet by using your models Manager. Each model has at least one Manager, and its called objectsby default. Access it directly via the model class, like so:

>>> Blog.objects>>> b = Blog(name='Foo', tagline='Bar')>>> b.objectsTraceback: ...AttributeError: "Manager isn't accessible via Blog instances."

Note

Managers are accessible only via model classes, rather than from model instances, to enforce a separation between table-level operations and record-level operations.

The Manager is the main source of QuerySets for a model. For example, Blog.objects.all() returns a QuerySetthat contains all Blog objects in the database.

Retrieving all objects

The simplest way to retrieve objects from a table is to get all of them. To do this, use the all() method on a Manager:

>>> all_entries = Entry.objects.all()
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Mastering Django Core»

Look at similar books to Mastering Django Core. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Mastering Django Core»

Discussion, reviews of the book Mastering Django Core and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.