Pages

Wednesday, May 24, 2017

Django Learning Notes

Django Learning Notes


Learning the Python Web Framework, Django, will be posting a series of reflections and notes on the framework in the future.

Django Database Modeling

from django.db import models


class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField(date published)


class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
#1 import the models class from django.db module

#3 defining a class here means defining a new table, the tables will be created if we run "python manage.py migrate"

#3 the Question class inherits from the Model subclass of the models class we imported

#4 attributes defined under a model.pys class will become a column in the generated table
#4 the models.Charfield() is not a method, but a class, by setting up the attribute on line 4, we are instantiating a new CharField object
#4 the "max_length = 200" is an attribute required by the class in order to instantiate the object, I think it is defined in the classes __init__ method.

#5 the DateTimeField is a similar class with an optional attribute to be set as its column name, in here, we are setting it to be "data published" so it is more human-friendly

#7 the Class Choice will create another table named Choice in the Database

#8 This line is very special, it creates a column of Foreign Key, and it will be mapped with the Question table
#8 I am not sure how the mapping is actually done, but I am guessing we will have to specify this field whenever we insert something into the Choice table.
#8 It is because the tutorial is trying to build a polling app, an option without a question will be meaning less...
#8 technically thinking, I suppose the foreign key can be missing, but it would render that row meaningless in our app.
#8 assuming that we are going to use SELECT * from Choice WHERE question == questionID
#8 An option row without a foreign key will never be useful because it will never be selected and displayed into a question polling interface.

#9 Nothing Special Here

#10 similar to other model.field class, but its for integer instead of character.
#10 not sure if the default attribute is required when instantiating an integerField object... I suppose it isnt?

Done for now, will add more in the future.

And yes, Ive abandoned the web.py framework - its nice to use "Pure Python" code as templating language, but well, its not exactly the same, and it is not really that supported.

Django, on the other hand, is a framework that is actively under development and heavy usage by a much larger community - I guess I am going with the Majority this time.

Think of which - by using jQuery, I have actually chosen something that is used by the majority as well - if I want to be special, I wouldve used vanilla javascript, right?

I love the experience and simplicity of jQuery, it saved me so many time, I hope Django would bring a similar experience!

I am kind of excited.

UPDATE:

SO-- Yes, I have just checked the 0001_initial.py(under "poll/migrations) which is generate by running "python manage.py makemigrations polls" in the project directory root, from there, we have a better view to the data structure generated by Django.

Codes are as follow

        migrations.CreateModel(
            name=Choice,
            fields=[
                (id, models.AutoField(verbose_name=ID, serialize=False, auto_created=True, primary_key=True)),
                (choice_text, models.CharField(max_length=200)),
                (votes, models.IntegerField(default=0)),
            ],
            options={
            },
            bases=(models.Model,),
        ),

From here, we can see that the method used to create a database column here added the ID automatically to the model it creates, and it is also used as primary key automatically by default (its not like we can change that without changing the source code anyway)

Also, relational columns, a.k.a foreign keys are added into the model after all the tables are created, which kind of make sense, and it is added with the following code.

        migrations.AddField(
            model_name=choice,
            name=question,
            field=models.ForeignKey(to=polls.Question),
            preserve_default=True,
        ),

So, mystery solved, moving onto next stage!


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.