Search code examples
pythondjangowagtail

django.core.exceptions.FieldError: Unknown field(s) (title) specified for Customer


I am trying to register a non-Wagtail model as a snippet, and to allow the model to be created and saved from another snippet's interface.

I have been getting this error and am not sure where to start. All of the other StackOverflow posts I've seen are specific to customizing the User model, which I am not doing.

I've tried setting a title field for the snippet, as well as appending a 'title' to get_context.

This is what I'm trying to accomplish, and I'm not certain if I'm doing it in the best way: There are customers and work orders. Each Work Order has a foreign key to a customer, and my goal is to be able to create customer models inline (or select from existing list) in the WorkOrder snippet interface.

Customer Snippet

@register_snippet
class Customer(models.Model):
    """Customer model."""

    customer_name = models.CharField(max_length=100, blank=False, null=False, help_text="John Doe")
    ...more regular customer info fields (address, city, state, etc.)...
    customer_home_phone = models.CharField(max_length=15, blank=False, null=False, help_text="Home Phone")

    panels = Page.content_panels + [
        MultiFieldPanel(
            [
                FieldPanel("customer_name"),
                FieldPanel("customer_home_phone"),
            ],
            heading="Customer Contact"
        ),
    ]

WorkOrder Snippet

@register_snippet
class WorkOrder(ClusterableModel, models.Model):
    """Workorder model."""

    STATUS_CHOICES = (
        ('o', 'Open'),
        ('c', 'Closed'),
        ('x', 'Cancelled'),
    )


    ...workorder fields (service address, city, state, etc)...
    status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='o')

    related_customer = models.ForeignKey(Customer, null=False, blank=False, on_delete=models.CASCADE)

    panels = [
        FieldPanel('status'),
        SnippetChooserPanel('related_customer'),
        MultipleImagesPanel('workorder_images', label="Relevant Images", image_field_name='image'),
    ]

class WorkOrderImage(Orderable):
    """WorkOrder Image Intermediary"""

    page = ParentalKey(WorkOrder, on_delete=models.CASCADE, related_name='workorder_images')
    image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    panels = [
        ImageChooserPanel('image'),
    ]

I have also included my multi-image chooser, though I don't see it being relevant to the issue.

I'm looking not only to solve this problem but also for some suggestions on how best to tackle the issue of creating a customer and saving it at the same time as a work order.

The issue I'm getting when I click "Add a customer" on the Customer snippet interface is django.core.exceptions.FieldError: Unknown field(s) (title) specified for Customer

I am thinking perhaps maybe I need to make my Customer model a ClusterableModel and not my Workorders.

Here is my traceback.

Internal Server Error: /admin/snippets/workorders/customer/add/
Traceback (most recent call last):
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/wagtail/admin/urls/__init__.py", line 110, in wrapper
    return view_func(request, *args, **kwargs)
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/wagtail/admin/auth.py", line 188, in decorated_view
    return view_func(request, *args, **kwargs)
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/wagtail/snippets/views/snippets.py", line 140, in create
    form_class = edit_handler.get_form_class()
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/wagtail/admin/edit_handlers.py", line 350, in get_form_class
    return get_form_for_model(
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/wagtail/admin/edit_handlers.py", line 62, in get_form_for_model
    return metaclass(class_name, (form_class,), form_class_attrs)
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/wagtail/admin/forms/models.py", line 75, in __new__
    new_class = super(WagtailAdminModelFormMetaclass, cls).__new__(cls, name, bases, attrs)
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/modelcluster/forms.py", line 234, in __new__
    new_class = super(ClusterFormMetaclass, cls).__new__(cls, name, bases, attrs)
  File "/Users/joseph/Python/PC_Work_Order/venv/lib/python3.9/site-packages/django/forms/models.py", line 268, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (title) specified for Customer
[20/Dec/2020 02:09:11] "GET /admin/snippets/workorders/customer/add/ HTTP/1.1" 500 104388

Solution

  • The problem is this line of the Customer class:

    panels = Page.content_panels + [...]
    

    The Page model defines a 'title' field in content_panels, but you don't have one. You probably want this to be simply panels = [...] instead.