CRUD Operations In Django

Create a new project by executing the following command.

  1. django-admin startproject CRUD
  2.     
  3. # CRUD is the name of project  

And execute the following command to start app name CRUD_FBVs.

  1. python manage.py startapp CRUD_FBVs

Don’t forget to add your new app to the Installed app. Append CRUD/settings.py as follow,

  1. INSTALLED_APPS = [
  2. 'django.contrib.admin',  
  3. 'django.contrib.auth',  
  4. 'django.contrib.contenttypes',  
  5. 'django.contrib.sessions',  
  6. 'django.contrib.messages',  
  7. 'django.contrib.staticfiles',  
  8. 'CRUD_FBVs',  
  9. ]  

Let’s start by creating our movie model. Open CRUD_FBVs/models.py and add the following code to it.

  1. from django.db import models
  2. from django.urls import reverse
  3. from django.contrib.auth.models import User
  4.   
  5.   
  6. class Movies(models.Model):
  7.   
  8. '''''by specifying user as Foreign Key, we're using the builtin User model of django.'''  
  9.   
  10. user = models.ForeignKey(User, on_delete=models.CASCADE)
  11. title = models.CharField(max_length=200)  
  12. genre = models.CharField(max_length=200)  
  13.   
  14. def __unicode__(self):  
  15. return self.title
  16.   
  17. def get_absolute_url(self):  
  18. return reverse('CRUD_FBVs:movies_edit', kwargs={'pk': self.pk})

We simply defined the model Movies by defining two fields, title and genre. We also defined user as the Foreign Key so that we’ll be able to use the built-in user model. By defining __unicode__() method, Django will call it when it needs to render an object in a context where a string representation is needed. get_absolute_url() method will tell Django how to calculate the canonical URL for an object. In other words, this method will return a string that can be used to refer to the object over HTTP.

Now apply the migrations as follows:

  1. python manage.py makemigrations

 and

  1. python manage.py migrate

 Let’s register our model to admin interface. Modify CRUD_FBVs/admin.py file as follow,

  1. from django.contrib import admin
  2. from .models import Movies
  3.    
  4.    
  5. admin.site.register(Movies)

 We also need to create a simple form to perform the CRUD operations. Create a new python file inside your app and name it forms.py. Append the following code to it.

  1. from django import forms
  2. from .models import Movies
  3.    
  4.    
  5. class MoviesForm(forms.ModelForm):
  6.    
  7.      class Meta:
  8. model = Movies
  9. fields = ['title', 'genre']  

 Function Based views uses decorators to achieve the special functionality. Let’s see how that works. Edit views.py file as follow:

  1. from django.contrib.auth.decorators import login_required
  2. from django.shortcuts import render, get_object_or_404, redirect
  3. from .forms import MoviesForm
  4. from CRUD_FBVs.models import Movies
  5.   
  6.   
  7. @login_required  
  8. def movies_list(request):
  9.      if request.user.is_superuser:
  10. movies = Movies.objects.all()
  11.      else:  
  12. movies = Movies.objects.filter(user=request.user)
  13.      return render(request, 'movies_list.html', {  
  14.          'object_list': movies
  15.      })  
  16.   
  17.   
  18. @login_required  
  19. def movies_create(request):
  20. form = MoviesForm(request.POST or None)  
  21.    
  22.      if form.is_valid():
  23. movies = form.save(commit=False)  
  24. movies.user = request.user
  25. movies.save()
  26.          return redirect('CRUD_FBVs:movies_list')  
  27.      return render(request, 'movies_form.html', {'form': form})
  28.   
  29.   
  30. @login_required  
  31. def movies_update(request, pk):
  32.      if request.user.is_superuser:
  33. movies = get_object_or_404(Movies, pk=pk)
  34.      else:  
  35. movies = get_object_or_404(Movies, pk=pk, user=request.user)
  36. form = MoviesForm(request.POST or None, instance=movies)
  37.      if form.is_valid():
  38. form.save()
  39.          return redirect('CRUD_FBVs:movies_list')  
  40.      return render(request, 'movies_form.html', {'form': form})
  41.   
  42.   
  43. @login_required  
  44. def movies_delete(request, pk):
  45.      if request.user.is_superuser:
  46. movies = get_object_or_404(Movies, pk=pk)
  47.      else:  
  48. movies = get_object_or_404(Movies, pk=pk, user=request.user)
  49.      if request.method == 'POST':  
  50. movies.delete()
  51.          return redirect('CRUD_FBVs:movies_list')  
  52.      return render(request, 'confirm_delete.html', {'object': movies})

We imported @login_required decorator which will limit the functionality of the CRUD operations to the logged in user. Remaining code is pretty simple and self-explanatory. You might have noticed the templates mentioned in the above code which we haven’t created yet. Let’s do that. Create templates folder in your app and create the HTML files as follow,

  1. # movie_list.html  
  2.  
  3.  

    CRUD Function Based View Example

      
  4.  
        
    •      {% for movies in object_list %}
    •      
    • {{ movies.title }}  {{ movies.genre }}  
    •      edit
    •      delete
    •      
    •   
    • {% endfor %}
    •  
      
  5.    
  6.  Add

#movies_form.html

  1. # movies_form.html  
  2.    
  3. CRUD Function Based View Example

      
  4.    
  5. {% csrf_token %}
  6. {{ form.as_p }}
  7.        
  8.    

 #movies_delete.html

  1. #confirm_delete.html  
  2.   
  3.  

    CRUD Function Based View Example

      
  4.    
  5. {% csrf_token %}
  6. Are you sure you want to delete "{{ object }}" ?  
  7.        
  8.    

 Now is the time to specify the URLs. Add the following code to urls.py:

  1. from django.conf.urls import url
  2.  from django.contrib import admin
  3.  from CRUD_FBVs import views
  4.    
  5. app_name = 'CRUD_FBVs'  
  6.    
  7. urlpatterns = [
  8. url('admin/', admin.site.urls),
  9. url(r'^$', views.movies_list, name='movies_list'),  
  10. url(r'^new$', views.movies_create, name='movies_new'),  
  11. url(r'^edit/(?P\d+)$', views.movies_update, name='movies_edit'),  
  12. url(r'^delete/(?P\d+)$', views.movies_delete, name='movies_delete'),  
  13.  ]  

That’s it. Now your app is ready to be up and running. Start your server by either executing the following command or directly by running it from your IDE.

  1. python manage.py runserver  

You can create superuser and use that to test the functionality or feel free to extend it.

Is there any con?

Function based views are easy to read and simple to implement but there’s also a downside to it. They are hard to customize or extend the functionality. Also they don’t allow the code reuse in true sense so repetitiveness still exists. To deal with all these issues, Class Based Views were introduced.

CRUD using Class Based Views

CRUD operations can be implemented in no time using CBVs, one of the biggest advantages being that if model evolves, changes would be automatically reflected in CBVs if done properly. Thus you can save tens of lines of code by implementing CBVs. They are easily extendable and also allow code reuse. Moreover, Django has built-in generic CBVs which make the life of a developer easy. Let’s implement them and see how the things turn out to be.

We are not going to just dump whatever we have done so far so keep calm. We just need to change our views and urls file as follow.

  1. from django.views.generic import ListView
  2.  from django.views.generic.edit import CreateView, UpdateView, DeleteView
  3.  from django.urls import reverse_lazy
  4.    
  5.  from CRUD_CBVs.models import Movies
  6.    
  7.    
  8.  class MoviesList(ListView):
  9. model = Movies
  10.    
  11.    
  12.  class MoviesCreate(CreateView):
  13. model = Movies
  14. fields = ['title', 'genre']  
  15. success_url = reverse_lazy('CRUD_CBVs: movies_list')  
  16.    
  17.    
  18.  class MoviesUpdate(UpdateView):
  19. model = Movies
  20. fields = ['title', 'genre']  
  21. success_url = reverse_lazy('CRUD_CBVs: movies_list')  
  22.    
  23.    
  24.  class MoviesDelete(DeleteView):
  25. model = Movies
  26. success_url = reverse_lazy('CRUD_CBVs: movies_list')  

We imported the generic Create, Update and Delete Views from Django which are pretty much performing all the CRUD operations. Now let’s edit urls.py file as follows:

  1. from django.conf.urls import url
  2.  from django.contrib import admin
  3.  from CRUD_CBVs import views
  4.    
  5. app_name = 'CRUD_CBVs'  
  6.    
  7. urlpatterns = [
  8. url('admin/', admin.site.urls),
  9. url('', views.MoviesList.as_view(), name='movies_list'),  
  10. url(r'^new$', views.MoviesCreate.as_view(), name='movies_new'),  
  11. url(r'^edit/(?P\d+)$', views.MoviesUpdate.as_view(), name='movies_edit'),  
  12. url(r'^delete/(?P\d+)$', views.MoviesDelete.as_view(), name='movies_delete'),  
  13.  ]  

That’s all for the changes. Now your app can perform all the CRUD operations easily.

你可能感兴趣的:(Django,Python)