mmckinlay
mmckinlay

Reputation: 67

How do I display foreignkey as a search option in Django cms Plugin

I want to be able to use the autocomplete_fields interface from Django admin in a Django CMS plugin

I've created a plugin which has a foreignkey field :

class ProductBlock(CMSPlugin):
    text = models.TextField()
    product = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True)

When I go to add the plugin to the page the option is shown as a dropdown.

I've created an admin.py as follows:

class ProductBlockAdmin(admin.ModelAdmin):
    autocomplete_fields = ['product']

admin.site.register(ProductBlock, ProductBlockAdmin)

In the admin section the field appears with the search option but this isn't displayed in the editor for the plugin.

I'm using django cms 4.1.3

Upvotes: 1

Views: 46

Answers (1)

Fabian
Fabian

Reputation: 850

I guess, the simplest way is to do the following

class ProductBlockPlugin(CMSPluginBase):
    autocomplete_fields = ['product']
    ...

Django CMS plugins do not register their own ModelAdmin, since they already are a subclass of ModelAdmin and already have the autocomplete functionality built-in.

No thinking about jQuery versions, or adding a dependency.

Upvotes: 0

Related Questions