Atul Bhatia
Atul Bhatia

Reputation: 1795

django admin custom model admin url

So I have a Bookings model in my django app. I am using django admin to display information about the booking. I want to create a custom url directly from the changelist view -- (not the change view).

I want the url to be something like: /admin/bookings/generate_bookings.

I've registered that url in my model admin by overwriting the get_urls function. The issue is that django automatically resolves any url that follows the syntax of model_name/<text> as a detail view url and it assumes whatever is in text is a primary key of the given model.

As a result I get the error:

booking object with primary key u'random_text' does not exist.

Is there any way to get my custom url to resolve first?

Upvotes: 1

Views: 1845

Answers (1)

doniyor
doniyor

Reputation: 37934

if you do:

def get_urls(self):
    urls = super(BookingAdmin, self).get_urls()
    my_urls = patterns('',
        (r'^generate_bookings/$', self.your_custom_function)
    )
    return my_urls + urls

and in changelist template somewhere:

<a href="generate_bookings/">Generate Booking</a>

it should work. It always worked for me at least

Upvotes: 1

Related Questions