Content Field5:06 with Kenneth Love
Our steps have a description but don't actually have any content. Let's fix that!
blank=True - A field can be blank (not filled in) in the admin and any other forms based on the model.
default='something' - If no value is supplied for the field, the default
'something' will be put into the record.
If you said migrate when I asked what else we have to do, you're right. 0:00 Good job remembering that dance. 0:04 You really do get used to the steps pretty quickly. 0:05 Okay, let's see about adding that field. 0:08 Since we have some existing rows in the database, 0:10 we're going to have to do something extra. 0:12 You know what, let's just do it. 0:13 Let's open up our models.py. 0:18 Let's add another text field to the step model. 0:21 So, it doesn't matter where we put this. 0:23 It's not going to care. 0:26 So we're going to put it in here, and this will be models.textfield. 0:29 Now we want to be sure and 0:33 call it content, because that's the name we've been using in our template. 0:35 Yeah we can change it here and 0:39 change the template, but who's gonna remember to do all that? 0:40 So, since we changed the data model, we've changed the database. 0:43 Right? Or, we want to. 0:49 Then, we should also change the database with a migration. 0:51 So, we're gonna CONTROL- C to quit. 0:58 And, if you remember how to do this, you're gonna makemigrations for courses. 1:03 And, we're gonna run this, and we just got a message. 1:10 We haven't gotten this message before. 1:13 Let's read this and see what this says. 1:14 So we're trying to add a non-nullable field content to step without a default. 1:16 Can't do that, the database needs something to populate existing rows, so 1:23 please provide a fix. 1:26 I have to provide a one off default now, 1:27 which would be set on all of the existing rows. 1:29 Or quit, and add a default in models.py. 1:31 The problem is that we've added this field, our content field, and 1:35 it can't be null. 1:38 Since this is just listed as a text field, there has to be something in that field. 1:39 Or Django has to know what to do with it, if there's nothing in that field. 1:43 That would be fine if we didn't have existing rows in the database, but we do. 1:47 We have some steps that we've already made. 1:51 So, Django doesn't know what to do with the rows that are already in the database. 1:54 Our database doesn't know what to let Django do with the rows that 1:58 are already in the database. 2:00 So basically Django and 2:01 the database have thrown up their hands,and going hey tell us what to do. 2:02 So we can either provide an existing default for 2:07 all the ones that have already been put in. 2:10 And that'll only happen now. 2:13 It won't happen when we run it on another database. 2:14 Or, we can quit, and we can set a default in the model. 2:17 And that will happen no matter what. 2:22 I think we should quit and change the model. 2:25 So, I'll hit control two, I guess I should hit two, but whatever. 2:27 Most of the time we would let our field be nullable. 2:33 Which means that it can hold no data. 2:36 But since this is a text field, that creates 2:38 a weird way of having two false values for empty text, or character fields. 2:41 We don't want that. 2:46 We want one single obvious explicit way of having a false value. 2:47 So, let's just set that. 2:52 What we're gonna do is we're gonna come over here to our tex' field, and 2:54 we're gonna set, blank equals true. 2:57 And that controls the form in our admin. 2:59 So, it says, hey, you don't have to get a value here. 3:03 And the default, if they don't put anything in, 3:06 is going to be an empty string. 3:09 So the worst case scenario, 3:11 the nobody provided any data scenario, is an empty string. 3:14 Which is fine, an empty string is a little bit of data. 3:18 And it can be blank in the form, we don't have to have value in the form. 3:20 Okay, so, let's try making this migration again. 3:25 Cool, it made the migration. 3:31 And now we can go ahead and migrate our model. 3:32 There we go. Done and it's okay. 3:41 No problems. 3:43 So, let's go add some content to our step in the admin. 3:44 So that means we need to run server again. 3:48 And let's see, we're looking at using the shell. 3:52 So let's refresh this page. 3:56 Now we have a content field for each of these. 3:59 So here's using the shell, here's our content. 4:02 So let's add in a little bit of stuff here. 4:04 So we'll say launch the shell with python. 4:07 You can exit the shell by entering exit or Quit. 4:16 You can also exit the shell by pressing Ctrl+D. 4:27 Okay. 4:33 So let's go save that. 4:34 And let's come over here and refresh our page. 4:38 And here's each of our lines. 4:40 And, if we inspect one of these. 4:42 We see <p> tags around it. 4:48 So that's great, we got paragraphs just like we wanted. 4:49 You might want to go back through our other templates, and 4:53 put the line breaks filter into places where we printed out the descriptions. 4:55 You know something we've been missing through this entire course? 4:58 We don't have a single link anywhere. 5:01 We should use anchor tags. 5:03 They're a great part of HTML. 5:04
You need to sign up for Treehouse in order to download course files.Sign up