A
A
Andrey Kovalchuk2017-04-18 16:05:49
Django
Andrey Kovalchuk, 2017-04-18 16:05:49

How to display editing of N materials on one page?

Good day.
So what do we have: model Post(models.Model)

class Post(models.Model):
    title = models.CharField(verbose_name='Заголовок', max_length=100, null=False, unique=True)
    content = models.TextField(verbose_name='Текст')
    min_age = models.IntegerField(verbose_name='Возрастной рейтинг', default=0)
    image = models.ImageField(verbose_name='Основное изображене', upload_to='news_image')
    site_source = models.URLField(verbose_name='Сайт источник', max_length=110)
    date_publish = models.DateTimeField('Дата публикации', null=True)
    category = models.ForeignKey(Category, verbose_name='Категория', on_delete=models.CASCADE, default=None)
    publish_status = models.BooleanField(verbose_name='Опубликован', default=False)
    tags = models.ManyToManyField(Tag)
    depend_posts = models.ManyToManyField('self')

    def __str__(self):
        return self.title

What I want to do: I want to display N filling forms in one view, where N = 1+len(depend_posts).
Preferably using CBV. For now, the process looks like this:
class UpdatePostView(UpdateView):
    model = Post
    form_class = PostForm
    template_name = 'mainApp/post_edit_create.html'
    success_url = '/admin/news/'

    def get_context_data(self, **kwargs):
        context = super(UpdatePostView, self).get_context_data(**kwargs)
        context['form_vk'] = PostForm
        context['depend_posts'] = self.model.objects.get(id=self.kwargs['pk']).depend_posts.values_list()
        return context

Those. I get one filled form and three empty ones, from which I don't know what to expect.
Can you please tell me how to implement the idea?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2017-04-18
@deliro

See how it's done in admin inlines.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question