Within the Model Admin, is it possible to control what's shown in a ManyToManyField horizontal_filter widget, in both the available and chosen fields?
For instance, I have a County model and a Territory model, and want to hide a County if it's already assigned to a Territory.
The problem with the below is that selected County objects will be hidden in both the "Available" and "Chosen" widget with horizontal_filter. Is it possible to only hide them in the "Available" column?
class TerritoryAdmin(admin.ModelAdmin):
filter_horizontal = ('counties',)
ordering = ('territory_name', )
# remove counties already assigned to a territory
def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == 'counties':
assigned = Territory.objects.all(
).values_list('counties__fips_code', flat=True)
kwargs["queryset"] = County.objects.exclude(fips_code__in=assigned)
return super().formfield_for_manytomany(db_field, request, **kwargs)
from Modifying Available and Chosen Choices For ManyToMany Field With Horizontal Filter In Django Model Admin
No comments:
Post a Comment