[Answer]-Django admin inline conditional values

1๐Ÿ‘

โœ…

I found a solution at http://www.stereoplex.com/blog/filtering-dropdown-lists-in-the-django-admin

class DescriptionsInline(admin.TabularInline):
    model = Descriptions
    exclude = ['modified']
    extra = 0
    def formfield_for_dbfield(self, field, **kwargs):
        if field.name == 'descriptiontype':
            parent_business = self.get_object(kwargs['request'], Business)
            if parent_business == None:
                related_descriptiontype = DescriptionType.objects.all()
            else:
                related_descriptiontype = DescriptionType.objects.filter(category=parent_business.category_id)
            return forms.ModelChoiceField(queryset=related_descriptiontype)
        return super(DescriptionsInline, self).formfield_for_dbfield(field, **kwargs)


    def get_object(self, request, model):
        object_id = request.META['PATH_INFO'].strip('/').split('/')[-1]
        try:
            object_id = int(object_id)
        except ValueError:
            return None
        return model.objects.get(pk=object_id)
๐Ÿ‘คTk421

Leave a comment