Skip to content

Instantly share code, notes, and snippets.

@luiscberrocal
Last active October 25, 2018 21:55
Show Gist options
  • Save luiscberrocal/9d2ed11c8ec1a0f01b07 to your computer and use it in GitHub Desktop.
Save luiscberrocal/9d2ed11c8ec1a0f01b07 to your computer and use it in GitHub Desktop.
Django Crud Views
class $MODEL$ListView(LoginRequiredMixin, ListView):
model = $MODEL$
context_object_name = '$context_object$_list'
def get_queryset(self):
return $MODEL$.objects.all()
class $MODEL$UpdateView(LoginRequiredMixin, UpdateView):
model = $MODEL$
context_object_name = '$context_object$'
form_class = $MODEL$Form
success_url = reverse_lazy('$namespace$:list')
class $MODEL$CreateView(LoginRequiredMixin, CreateView):
model = $MODEL$
context_object_name = '$context_object$'
form_class = $MODEL$Form
success_url = reverse_lazy('$namespace$:list')
def get_context_data(self, **kwargs):
context = super($MODEL$CreateView, self).get_context_data(**kwargs)
#context['form'].fields['employee'].queryset = Employee.objects.from_group(self.kwargs['group_slug'])
return context
def form_valid(self, form):
#obj = form.save(commit=False)
#employee = Employee.objects.get(user=self.request.user)
#obj.coach = employee
#obj.save()
return super($MODEL$CreateView, self).form_valid(form)
class $MODEL$DetailView(LoginRequiredMixin, DetailView):
model = $MODEL$
context_object_name = '$context_object$'
class $MODEL$DeleteView(LoginRequiredMixin, DeleteView):
model = $MODEL$
success_url = reverse_lazy('$namespace$:list')
context_object_name = '$context_object$'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment