- Overview
- Transcript
4.3 Check for Duplicate Data
The last thing I want to look at is some common functionality to prevent duplicate data. If the user enters the same feed URL twice, we don't want to add duplicate data to our articles page. That would be annoying. So in this lesson, I will show you a simple way to avoid saving duplicate data to the database.
1.Introduction3 lessons, 14:42
1.1Introduction00:58
1.2Prerequisites04:14
1.3Set Up the Development Environment09:30
2.Foundations6 lessons, 47:44
2.1Creating Models09:20
2.2Migrations and Administration08:04
2.3Create Router URLs10:25
2.4Our First View04:48
2.5Create a Template05:46
2.6Adding Dynamic Data09:21
3.Add Structure6 lessons, 52:54
3.1Incorporate Bootstrap06:10
3.2Add a Feeds List Page11:11
3.3Add a Base Template07:10
3.4Create a Feed Form09:28
3.5Handle POST Data06:07
3.6Adding a Real Feed12:48
4.Clean Up3 lessons, 16:58
4.1Give Models a Meaningful Name04:24
4.2Format the Articles Page08:49
4.3Check for Duplicate Data03:45
5.Conclusion1 lesson, 01:23
5.1Goodbye01:23
4.3 Check for Duplicate Data
The next modification that I wanna make to our application has to do is what happens if I come into my application and I pasted a url in here that is a same url that already exist in my database and I hit save. Well what's gonna happen is that my application is gonna go out and is gonna grab that feed just like it did before. It's gonna create a new feed entry and a bunch of articles in there and they're all gonna be duplicates and that's gonna be very annoying to the end user as well as anybody that has to actually go back in and fix and clean up all of that data. So I'm gonna show you a way on how to avoid that. So let's come back into our application here and what I'd like to do is come into our new feed view and I want to check for that scenario. So the first that I wanna do is I wanna come in here after I've gotten my form and everything has been validated, and I've been able to convert that form into an actual feed model, I wanna be able to see if that already exists in there. So there's several different ways to do that. But I think one very easy way for us to do it is to take a look and see if the URL for that feed already exists within the database. So we'll simply say we want to create existing feed as a variable and we'll simply come in here and say feed. I want to grab the objects but instead of doing all this time, I don't want to do that, I want to filter my results, I want to filter on certain properties and values. This is kinda the equivalent of creating that wearer clause if you were to write some SQL queries. So in here I wanna say I want to filter, and I wanna grab where the URL is equal to feed.url. Now one thing to keep in mind here is that filter is going to return an additional query set. So it's going to return a series of results, not a single result. Even if you're specifying something in here that is uniquely identifying a single row, it's always going to return it in a collection. Now you can do something like get that will give you a single value, so you can search for a single value. But the problem with this approach will be that if there are multiple values in the database that match that you're gonna get an error. So I'm simply gonna stick with filter for now, knowing that I'm gonna get back a collection or multiple values in this form, I can simply say if existing feed or actually if length of existing feed is equal to 0, then I know that, this doesn't exist in the database yet. And then I can go ahead and run all of this code. So I'll just cut this and I'll bring it up in here, paste it in making sure I keep my indenting like so. And I'll save this. And so now what's gonna happen is it's gonna check to see if there's already a feed or multiple feeds possibly that has a URL of what I've already entered in there. And if it does not, if the length of that query set is zero, then I'll go ahead and go through the process of creating it, just like I did before, and then either way I'm going to redirect back to the feed list. So let's make sure that's saved, and we'll come back in here. Our URL is already inserted into our text box, and we'll go ahead and hit save. Now we've come right back here very quickly. So obviously something happened in that it noticed that something in the data base all ready existed with that URL. So skip right over and say all right, you know what, I all ready got it in the database. Don't worry about it. And that's definitely a nice feature to be able to add to your web site. So that's the basics of getting started in creating a very simple news aggregator using Django, using Python and being and being able to go out and use some basic third party libraries to retrieve some information that might be useful and presenting it to the end user in a pretty meaningful way.