Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Whats up with PK's? - Django course Views

So my question is the following I have done the Django course and specially when adding instances to a Model the views must have a Pk of the table if I did understand correctly, but Kenneth didn't explain why we needed them the only thing I got out of it was that its the prime key of the table.

Anyone can explain me in a better detail why the PK is so important, how to use it and when to assign it?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

All database tables must have a mechanism to sort the entries within the table. If a model does not specify an attribute to be the primary key (pk), the database will automatically add an id column to each entry. This id column will be marked as the default primary key for the table. "pk" is a reference to whichever column is designated as the primary key column.

When creating a new instance and saving to the database, a pk is automatically generated and saved along with the new entry.

The primary key (pk) is used when refer to a specific instance (entry) with in a table.

The "pk of the table" is not necessary in saving the form in your code because the form should be associated with a model within the definition of creationForm. The form is saved in this model's database table.

Post back if you need more details.

Hey chris thanks a lot for the reply, You see I am able to seed my DB with the forms, but I wanna make a search form so I can then edit them, and I haven't been able to accomplish this, django's course with kenneth is great but this topic hasn't been addressed. Do you have any idea on how I could accomplish this? my gut tells me I could use the pks

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The forms aren't saved as themselves. The data from the form is saved as an instance of the model the form was based on. It is these model's data that are retrieved from the database.

If the forms are based on the model MyModel, then all of the form data is in the table "MyModel"

all_models_from_forms = MyModel.objects.all()
for model in all_models_from_forms:
    # do something with model
    print("this model has pk: {}".format(model.pk))

# for a specific model use
some_model =  MyModel.objects.get(pk=pk)

# Or find the model with name Tomas
some_model =  MyModel.objects.get(name='Tomas')

# Or find all models created after a date
found_models = MyModel.objects.filter(creation_date__gt=timezone.now())

Check out the Database QuerySet API for more ways to get specific instances from the database.

So I made this view and it saved to my DB with no problem at all, I honestly do not get the usage of PK's

def creation_form(request):
    form = forms.creationForm()
    if request.method == 'POST':
        form = forms.creationForm(request.POST)
        if form.is_valid():
            form.save()
    return render(request, 'sgt_app/creation_form.html', {'form':form})
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

If I may give advice on posting questions, It's best to post additional information about your question as a Comment instead of an Answer. Posting as an Answer makes the question look "answered" and reduces the number of people viewing your question. I wandered in because I was curious what the answer was.