Hussam
Hussam

Reputation: 510

get_absolute_url() using app hook in Django

I have the following Django model:

class Product(models.Model):
    name    = models.CharField(max_length=250)
    slug    = models.SlugField(max_length=250, unique=True)
    active  = models.BooleanField(default=True)
    date    = models.DateTimeField(auto_now_add=True)

def get_absolute_url(self):
    return '/' +self.slug

And I attached it to a page using this apphook:

class ProductApphook(CMSApp):
    name = _("Products Apphook")
    urls = ["product.urls"]
apphook_pool.register(ProductApphook)

My question is about the get_absolute_url function: it will return an invalid url, since the slug of the page where this apphook is attached will not be prepended to the absolute_url in the models get_absolute_url function. Is there a way to make the get_absolute_url function return the correct url? I'm particularly interested in this because the 'Preview' function in the admin doesn't work for the above setup

I'm not quite sure this is possible, e.g. what if the apphook is attached to multiple pages?

Upvotes: 0

Views: 315

Answers (1)

droxey
droxey

Reputation: 84

I recommend creating a named url in your urls.py, and then reversing the url in your get_absolute_url function. See the Django docs for reverse().

Upvotes: 1

Related Questions