daniyalzade / django_reverse_admin

Django Reverse Admin
BSD 3-Clause "New" or "Revised" License
199 stars 54 forks source link

Allow Select2 dropdown for ForeignKeys in the reversed Model #281

Open ghost opened 3 years ago

ghost commented 3 years ago

Let's say you have the following model structure, modified from the README example:

class Street(models.Model):
    street_name = models.CharField(max_length=255)

class Address(models.Model):
    street = models.ForeignKey(Street)

class Person(models.Model):
    name = models.CharField(max_length=255)
    home_addr = models.OneToOneField(Address, related_name='home_addr')

If the amount of "Street" objects you have is too large, a Select2 dropdown is preferred, and currently there's no way of having them. Adding the fields to autocomplete_fields does nothing.

gabrielhruza commented 3 years ago

i'm having the same issue :/

AverageS commented 3 weeks ago

Perhaps a little bit too late, but you can specify admin class in inline_reverse , for example using example you supplied:

models.py

class Street(models.Model):
    street_name = models.CharField(max_length=255)

class Address(models.Model):
    street = models.ForeignKey(Street)

class Person(models.Model):
    name = models.CharField(max_length=255)
    home_addr = models.OneToOneField(Address, related_name='home_addr')

admin.py:

.....
class AddressInlineAdmin(admin.StackedInline):
    model = Address
    autocomplete_fields = ['street']

class PersonAdmin(ReverseModelAdmin):
    inline_type = 'stacked'
    inline_reverse = [{'field_name': 'home_addr', 'admin_class': AddressInlineAdmin}]

......