iommirocks / iommi

Your first pick for a django power cord
http://iommi.rocks
BSD 3-Clause "New" or "Revised" License
767 stars 51 forks source link

When a Page features more than one EditTable, the Save feature breaks sometimes if the order of the EditTables differs from the order of the Models in the models.py file. #588

Open leethobbit opened 2 days ago

leethobbit commented 2 days ago

Issue: When using multiple EditTables on a page, and when the order of the EditTables is different from the order that the models appear in the models.py file, the Save feature of the 2nd table seems to break and no longer submit new rows.

In my example, I have models for Role and Person at a business. The Role model appears before the Person model in the models.py.

Code in use: models.py

from django.db import models

class Role(models.Model):
    name = models.CharField(max_length=80, unique=True)
    description = models.TextField(blank=True, null=False, default="")

    def __str__(self):
        """
        Required method to see the name field when a form is created with this model
        """
        return self.name

    def get_absolute_url(self):
        return f"/people/roles/{self.id}/"

# class Person
# TODO Sort out how to link donations to a person, and sum them
class Person(models.Model):
    first_name = models.CharField(max_length=80, null=False, default="")
    last_name = models.CharField(max_length=80, null=False, default="")
    email = models.EmailField(null=False, default="donotemail@example.com")
    phone_number = models.CharField(max_length=20, default="555-5555")
    roles = models.ManyToManyField(Role)
    address = models.CharField(max_length=250, default="")
    zip_code = models.IntegerField(default=44121)
    notes = models.TextField(blank=True, null=False, default="")

    def __str__(self):
        """
        Required method to see the name field when a form is created with this model
        """
        return self.first_name + " " + self.last_name

    def get_absolute_url(self):
        return f"/people/{self.id}/"

views.py

from iommi import Action

from oar.core import EditTable

from .models import Person
from .models import Role

# Tables
people_table = EditTable(
    auto__model=Person,
    title="",
    actions__create_person=Action(
        attrs__href="/people/create/",
        attrs__class={"btn": True, "btn-info": True},
    ),
    attrs__class={"panel": True},
    # Search filters
    # Turn on edit feature for columns
    # Style Attributes
)

roles_table = EditTable(
    auto__model=Role,
    title="",
    actions__create_role=Action(
        attrs__href="/people/roles/create/",
        attrs__class={"btn": True, "btn-info": True},
    ),
)

urls.py

from django.shortcuts import get_object_or_404
from django.urls import path
from iommi import Form
from iommi import Page
from iommi import html

import oar.people.views as people_views

from .models import Person
from .models import Role

app_name = "people"

# Menu

# Tables

# Pages
class PeopleIndex(Page):
    people_title = html.h1("People")
    hr1 = html.hr("")
    people = people_views.people_table
    br2 = html.br("")
    roles_title = html.h1("Roles")
    hr2 = html.hr("")
    roles = people_views.roles_table  # The order here causes a problem

If I place them on a Page with the Role EditTable first, things work as expected. If I flip them, the Role EditTable (which now appears second) will no longer save new rows. I was able to replicate this with multiple other models from different apps in my project so it seems consistent across the board.

Hope I have provided enough details to narrow down the issue. If I discover more I'll add to this post.

boxed commented 2 days ago

I tried with this:

    path('test/', Page(
        parts=dict(
            a=EditTable(auto__model=Department, columns__name__field__include=True),
            b=EditTable(auto__model=DocumentCategory, columns__name__field__include=True),
            c=EditTable(auto__model=Document, columns__name__field__include=True),
        )
    )),

which works, so it's not trivially reproducible at least.

Are you running the latest version of iommi?

leethobbit commented 2 days ago

Interesting. I tested with both 7.5.1 and 7.6.0 and encounter the situation in both versions. I've been using classes to define my index Pages, which is where the trouble shows up.

Additionally, it seems there are more inconsistencies here than I noticed previously. One of my apps in this project is behaving as expected. So, I need to investigate further on my end.