How to use CreateView

3

I'm trying to create a simple task list, but I'm having some problems. I want to create a task, inside it I want to create some activities. This is the problem I can not do.

How can I create an activity using a CreateView , however I would be inside a task that was already created with CreateView . Now, in this other, I need to pass the pk of the task, creating an activity bound to it, and once it is created, how can I use reverse to return to detailview of this task?

models :

from django.db import models
from django.core.urlresolvers import reverse

class List(models.Model):
    title = models.CharField(blank=True, max_length=100)

class  Meta:
    ordering = ['id']

def __str__(self):
    return self.title

def get_absolute_url(self):
    return reverse('task-detail', kwargs={'pk': self.pk})


PRIORITY_CHOICES = (
    (1, 'Low'),
    (2, 'Normal'),
    (3, 'High')
)

class Item(models.Model):
    title = models.CharField(blank=True, max_length=100)
    created = models.DateTimeField(auto_now=False, auto_now_add=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    priority = models.IntegerField(choices=PRIORITY_CHOICES, default=2)
    completed = models.BooleanField(default=False)
    todo_list = models.ForeignKey(List)

class Meta:
    ordering = ['-priority', 'title']

views :

class TaskDetailView(DetailView):
    model = List

class TaskCreateView(CreateView):
    model = List
    fields = ['title']
    success_url = '/home'

class TaskUpdateView(UpdateView):
    model = List
    fields = ['title']
    template_name = 'core/update.html'
    success_url = '/home'

class TaskDeleteView(DeleteView):
    model = List
    success_url = reverse_lazy('home')


class ActivityCreateView(CreateView):
    model = Item
    fields = 'title', 'priority', 'completed'
    success_url = '/home'

class ActivityUpdateView(UpdateView):
    model = Item
    fields = 'title', 'priority', 'completed'
    template_name = 'core/update_actvity.html'
    success_url = '/home'

class ActivityDeleteView(DeleteView):
    model = Item
    success_url = reverse_lazy('home')

url :

from django.conf.urls import url, include
from django.contrib import admin
from core.views import ActivityCreateView, ActivityDeleteView, ActivityUpdateView
from core.views import TaskDetailView, TaskCreateView, TaskUpdateView, TaskDeleteView, Home, MyView


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('allauth.urls')),
    url(r'^$', Home.as_view(), name="index" ),
    url(r'^home/$', MyView.as_view(), name="home" ),
    url(r'^task/(?P<pk>[0-9]+)/$', TaskDetailView.as_view(), name='task-detail'),
    url(r'^task/(?P<pk>[0-9]+)/update/$', TaskUpdateView.as_view(), name='task-update'),
    url(r'^task/(?P<pk>[0-9]+)/delete/$', TaskDeleteView.as_view(), name='task-delete'),
    url(r'^task/add/$', TaskCreateView.as_view(), name='task-create'),

]
    
asked by anonymous 23.05.2017 / 06:46

1 answer

1

You can use CreateView with the inlineformset_factory feature. I made a post with an example about it, see if it helps: link

    
26.05.2017 / 16:54