ixc / wagtail-instance-selector

A widget for Wagtail's admin that allows you to create and select related items
MIT License
54 stars 17 forks source link

Using InstanceSelectorPanel with ClusterableModel #10

Open OBKF opened 4 years ago

OBKF commented 4 years ago

Hello, I am using InstanceSelectorPanel to provide a way to add ForeignKey values to a model with search (since the data may be huge later on), also I want to use Orderable to provide images to my model.

Now I have this error: "<Hotel: Hotel object (None)>" needs to have a value for field "id" before this many-to-many relationship can be used.

code:

class Place(models.Model):
    name = models.CharField(
        verbose_name=_("name"), max_length=128, blank=True, null=True)
    ...
    class Meta:
        abstract = True
        ordering = ['-date']

class Hotel(ClusterableModel, Place):
    panels = [
        MultiFieldPanel(
            [
                FieldPanel("name"),
                ...
            ],
            heading="base Options",
        ),
        MultiFieldPanel(
            [
                FieldPanel("rooms"),
                ...
                InstanceSelectorPanel("city"), <--------------------------------------------------------------
            ],
            heading="Other Options",
        ),
        MultiFieldPanel(
            [InlinePanel("images", max_num=10, min_num=1, label="Image")],
            heading="Images",
        ),
    ]

So is there any possible way I can go around this ?

Thanks.

markfinger commented 4 years ago

I'm not sure actually. I've never tried using ClusterableModel directly. From memory, wagtail Page models use ClusterableModel under the hood, but I've never had any problems with InstanceSelectorPanel on a Page's admin detail.

Maybe there's something missing in your admin wiring? How are you declaring the admin for the Hotel model?

markfinger commented 4 years ago

Another possibility, there might be some kind of ClusterableModel model hook in wagtail that we'd need to opt into handling. If so, I'd expect there's an isinstance check on the panel or a method/hook of some kind we'll need to add.

I don't have the time currently to dig into this myself, but I'm more than happy to help in any small ways. Providing context or explanations, etc.

OBKF commented 4 years ago

@markfinger I switched to AutocompletePanel (wagtail-autocomplete) for this type of models, its not as convenient but it works.

How are you declaring the admin for the Hotel model?

My AdminView is simple and it is the same as the other models (not ClusterableModel and they work fine):

class HotelAdmin(ModelAdmin):
    create_view_class = ServiceCreateView

    model = Hotel
    menu_label = "Hotels"
    menu_icon = "fa-hotel"
    menu_order = 100
    add_to_settings_menu = False
    list_display = ("name", "city", "available",)
    search_fields = ("name",)

I don't have the time currently to dig into this myself, but I'm more than happy to help in any small ways. Providing context or explanations, etc.

For now I am Ok with using wagtail-autocomplete, but I will dig more into it when I finish this project or I really need it. Thanks