Overview of Recent activities
I have not been keeping up this blog as much as I intended. Summer days seem to get away from me. I work in sweaty spurts and then go cool off by a fan. I mostly want to watch old videos. Still, I have been studying PostgreSQL and preparing a proposal for a text book on SQL using PostgreSQL. I just turned in the proposal on Saturday of this week.
Installing PostgreSQL was easy enough, but the pgAdmin was more difficult. I will publish a script for this later. Here though is a link to the pgAdmin documentation installing pgAdmin
I have also installed Django on Ubuntu. I previously installed it on Windows. I am using Microsoft's VSCode for my coding, I like it because it has a code window, a file manager and a terminal window all in view at the same time.
Installing Django was easy enough, as was setting up the virtual environment. Again I will publish a complete script for this later after I have done it enough times to be confident in the necessary features and sequence.
Things Learned the hard way
There are several things I struggled with that will not be a problem the next time, and I believe I can steer students through the process without them having to go through the frustrations that I did.
It takes a while to get used to the structure: env/project/project/files.It is not always clear where to put a file or where to add a new folder like templates.
I edited the settings.py and, in the database section managed to connect to the postgreSQL database, though I found I had to use pip3 to download and install the psycopg2. Note: You now must download psycopg2-binary. The code below does not change.
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'bookreview', 'USER': 'postgres', 'PASSWORD': 'P@ssw0rd1', 'HOST': 'localhost', 'PORT': '', }
I also needed to add an import statement for psycopg2 at the top of the file.
I added a models.py to my project at env/project/project/models.py.
from django.db import models class Book(models.Model): bookkey = models.IntegerField(primary_key=True) booktitle=models.CharField(max_length=255) bookpublishyear=models.IntegerField bookISBN=models.CharField(max_length=20) class Meta: db_table="book" class Author (models.Model): authorkey= models.IntegerField(primary_key=True) authorname=models.CharField(max_length=255) authorcountry=models.CharField(max_length=255) authordates=models.CharField(max_length=255) class Meta: db_table="author" class AuthorBook (models.Model): authorkey=models.ForeignKey(Author,on_delete=models.CASCADE) bookkey=models.ForeignKey(Book,on_delete=models.CASCADE) class Meta: db_table="authorbook" class Reviewer (models.Model): reviewerkey = models.IntegerField(primary_key=True) reviewerusername=models.CharField(max_length=255) revieweremail=models.CharField(max_length=255) class Meta: db_table="reviewer" class Review(models.Model): reviewkey = models.IntegerField(primary_key=True) reviewerkey=models.ForeignKey(Reviewer, on_delete=models.CASCADE) bookkey = models.ForeignKey(Book, on_delete=models.CASCADE) reviewdate=models.DateField() reviewtitle=models.CharField(max_length=255) reviewtext=models.TextField() class Meta: db_table="review"
I included the linking table, although there are ways to manage many-to-many relationships. I will need to explore them more carefully.
You use manage.py for almost everything in Django. First I created a superuser. That was easy enough. But I was still having trouble with admin and with migrating the model to Postgres. It took me way to long to realize that I had to register the project in the settings.py file.
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'project01' ]
My project is the last one registered there with the imaginative name of “project01.” Once I registered the project, I managed to migrate the tables to Postgres, but I still couldn't see them in the Admin. This involved creating another file "admin.py" in which I registered all the classes in my model.
from django.contrib import admin from project01.models import Book, Author, AuthorBook, Reviewer, Review admin.site.register(Book) admin.site.register(Author) admin.site.register(AuthorBook) admin.site.register(Reviewer) admin.site.register(Review)
Now the models showed up in the admin app.

Here is a shot of pgAdmin with the tables it has added. It is worth noting that it added a lot more than a table for each of the classes I made in the model.
Finally, I will add that I have also installed Apache, though I haven't connected Django to it yet. Here is a view of the whole screen.

No comments:
Post a Comment