reading notes for code fellows
Models are going to be an important part of working with SQL in this class, and the admin app will be helpful in getting our data to where we want more easily.
Django apps access and manage data through Python models, which define the structure of stored data. With this setup, once you’ve decided on your database, you don’t need to interact with it directly, as Django will handle database communication for you. Before you code out a local model, you should think about exactly what data it will be holding. Generally, you will want separate models for different groups of related information, and you can use models to code out things like drop down lists rather than hard coding. Once you know your models and field, you will need to consider the relationship, OneToOneField. ForeignKey, or ManyToManyField.
You will generally define models within a models.py file as a subclass of django.db.models.Model that can include fields, methods, and metadata. A model can have any number of fields, each one representing a column of data stored in a database table. Each row in the table will consist of one of each field value. Field types are assigned using specific classes that will determine the type of record being used to store data and the validation criteria to be used when it comes in from an HTML form. The name of the field is what it will be referred to as in queries. Some common field arguments are:
help_text: provides a text label for HTML formsverbose_name: A human-readable name for the field labeldefault: the default value for the fieldnull: if True, Django will store blank values as NULLblank: if True the field is allowed to be blank in your formschoices: a group of choices for the fieldprimary_key: if True, set the current field as the primary key for the modelCommon field types:
CharField: short to mid-sized fixed-length strings (max-length required)TextField: large arbitrary-length strings (max-length optional)IntegerField: stores integer valuesDateField/DateTimeField: stores date/time info as Python objectsEmailFiled: stores and validates email addressesFileField/ImageField: uploads files and imagesAutoField: a type of IntegerField that automatically incrementsForeignKey: specifies a one-to-many relationship to another modelManyToManyField: specifies a many-to-many relationshipYou can declare model-level metadata by declaring class Meta. One of the most useful features with this is the ability to control the default ordering of records being returned by specifying the match order. You also can create and apply access permissions for the model. Models themselves can also have methods. As the minimum, a model should have a __str__() method to return a human-readable string for each object, but another common method to include is get_absolute_url() which returns a url for displaying individual model records on the website.
Once you’ve defined your model class, you then can use it to create, update, or delete records, as well as running queries. To create a new model, you would use .save(). To update things within the model, you will want to use dot notation, and call save() again at the end to save your changes to the database. If you want to search for something, you can use the model’s object attribute. Items will come back as a QuerySet, which is an iterable object. filter() allows us to filter through a QuerySet to match specified text or numeric field criteria.
The Django admin app can use your models to automatically build a site area that will allow you to create, view, update and delete records, which can save a considerable amount of time during development. It is recommended only for internal use as a testing tool, as it is not necessarily a good interface for all users and shows unneccesary details. All you need to do to add your models to admin is register them by importing the models to admin.py and calling admin.site.register() on each of them. To log into the admin site, you will need a user account with staff status enabled, which will also need management permissions. For this, you can create a ‘superuser’ account that has full access to the site and all permissions in manage.py. To log into admin, you can open the /admin URL and enter the superuser id and password. This will bring you to the display of all the models where you can edit them and add things.
I imagine we’re going to be getting into using SQL in the coming days, and so we’re bringing this out. I enjoyed the bits of SQL we did in the prework, so I’ll be interested to see how we will be using it going forward.