Search code examples
pythondjangodjango-admin

Pass parameters in URL to Django Admin add_view


I need to pass a parameter in the URL to the Django Admin add view, so that when I type the URL: http://localhost:8000/admin/myapp/car/add?brand_id=1, the add_view reads the brand_id parameter. I want to use this so that I can set a default value for the brand attribute of Car.

def get_form(self, request, obj=None, **kwargs):
    form = super(CarAdmin, self).get_form(request, obj, **kwargs)
    form.base_fields['brand'].initial = <<GET['brand_id']>>
    return form

Use case: I want this because in my BrandsAdmin, I have added a "+" button for each brand, that should add a Car with that Brand as FK.

I've tried getting it from request in get_form, but when that code is executed, Django has already changed my URL as I guess it doesn't understand it as a "legal" parameter.

Thanks a lot!


Solution

  • Mainly, for what I wanted to do, it's as easy as two steps.

    First, add a column in list_display to show the link to the add_view:

    Brand.py

    @admin.display(description='Link', ordering='url')
    def get_add_link(self):
        url = f"{reverse('admin:myapp_car_add')}?brand={self.id}"
        return format_html(f"<a href='{url}'><i class='fas fa-plus'></i></a>")
    

    Note that I add the URL as the reverse of admin:<app>_<model>_<action> and pass the brand ID as parameter.

    CarAdmin.py

    def get_changeform_initial_data(self, request):
        brand_id = request.GET.get('brand')
        return {'brand': brand_id}
    

    By adding this method in the CarAdmin class, I am able to set the brand as the default value.