0

I got 1 app "portfolio" with 2 models, the 2nd one has a FK with first and I need (create and update) view of that. I have the complete CRUD for the first model but I no have idea to get the same result with fk model. I'm looking for a create and update view with both. But I can't get access for proyects in a album form, so in that way for choosing the FK from projects to get related album to one of all projects created

models.py

from django.db import models
from django.contrib.auth.models import User
from ckeditor.fields import RichTextField

# Create your models here.

class Project(models.Model):
    user = models.ForeignKey(User, on_delete = models.CASCADE, default=1)
    name = models.CharField(verbose_name='Nombre del proyecto', max_length=200)
    client = models.CharField(verbose_name='Nombre del cliente', max_length=200)
    description = RichTextField(verbose_name='Descripción')
    start = models.DateField(verbose_name='Fecha de Inicio', null=True, blank=True)
    ending = models.DateField(verbose_name='Fecha de Finalización', null=True, blank=True)
    order = models.SmallIntegerField(verbose_name="Orden", default=0)
    created = models.DateTimeField(verbose_name='Fecha de creación', auto_now_add=True)
    updated = models.DateTimeField(verbose_name='Fecha de modificación', auto_now=True)

    class Meta:
        verbose_name = 'Proyecto'
        verbose_name_plural = 'Proyectos'
        ordering = ['-start', 'order']

    def __str__(self):
        return self.name

class Album(models.Model):
    project = models.ForeignKey(Project, on_delete = models.CASCADE)
    title = models.CharField(verbose_name='Título de la imagen', max_length=200, null=True, blank=True)
    image = models.ImageField(verbose_name='Imagen', upload_to='portfolio')
    created = models.DateTimeField(verbose_name='Fecha de creación', auto_now_add=True)
    updated = models.DateTimeField(verbose_name='Fecha de modificación', auto_now=True)

    class Meta:
        verbose_name = 'Imagen en el album'
        verbose_name_plural = 'Imágenes en el album'
        ordering = ['created']

        def __str__(self):
            return self.title

views.py

@method_decorator(staff_member_required(login_url='login'), name='dispatch')
class ProjectCreateView(CreateView):
    model = Project
    template_name = "core/project_create_form.html"
    form_class = ProjectCreateForm
    success_url = reverse_lazy('home')

@method_decorator(staff_member_required(login_url='login'), name='dispatch')
class ProjectUpdateView(UpdateView):
    model = Project
    template_name = "core/project_update_form.html"
    form_class = ProjectUpdateForm


@method_decorator(staff_member_required(login_url='login'), name='dispatch')
class ProjectDeleteView(DeleteView):
    model = Project
    template_name = "core/project_delete_form.html"
    success_url = reverse_lazy('home')

forms.py

class ProjectCreateForm(forms.ModelForm):

    template_name = 'core/project_create_form.html'

    class Meta:
        model = Project
        fields = [
            'name',
            'client',
            'description',
            'start',
            'ending',
            'order',
            ]

        widgets = {

            'name':forms.TextInput(
                attrs={
                    'class':'form-control'}),

            'client':forms.TextInput(
                attrs={
                    'class':'form-control'}),

            'description':forms.Textarea(
                attrs={
                    'class':'form-control'}),

            'start':forms.DateInput(
                attrs={
                    'class':'form-control'}),

            'ending':forms.DateInput(
                attrs={
                    'class':'form-control'}),

            'order':forms.NumberInput(
                attrs={
                    'class':'form-control'}),
        }


class ProjectUpdateForm(forms.ModelForm):

    template_name = 'core/project_update_form.html'

    class Meta:
        model = Project
        fields = [
            'name',
            'client',
            'description',
            'start',
            'ending',
            'order',
            ]

        widgets = {

            'name':forms.TextInput(
                attrs={
                    'class':'form-control'}),

            'client':forms.TextInput(
                attrs={
                    'class':'form-control'}),

            'description':forms.Textarea(
                attrs={
                    'class':'form-control'}),

            'start':forms.DateInput(
                attrs={
                    'class':'form-control'}),

            'ending':forms.DateInput(
                attrs={
                    'class':'form-control'}),

            'order':forms.NumberInput(
                attrs={
                    'class':'form-control'}),
        }
nilsoviani
  • 175
  • 3
  • 12
  • So you’re looking to have both project and album forms on the same page? – Carl Brubaker Aug 27 '18 at 22:26
  • Yes, something like this: [Example](https://imgur.com/a/gsNccJh) – nilsoviani Aug 28 '18 at 01:56
  • I might not get you all the way there, but here are some links that I saved to help me. [inlineformsets](https://stackoverflow.com/questions/50771962/two-models-fields-in-one-form-in-django-2-0) and [multiple forms](https://stackoverflow.com/questions/5857363/using-multiple-forms-on-a-page-in-django). – Carl Brubaker Aug 28 '18 at 02:02

1 Answers1

0

I haven't tried this library but it might solve your problem, a common problem in Django is how to have a view, especially a class-based view that can display and process multiple forms at once. https://github.com/kennethlove/django-shapeshifter

aldyahsn
  • 1,170
  • 1
  • 7
  • 14