Search code examples
djangomodelssubclassingsitemap.xml

Overriding get_absolute_url from a model for the sitemap


Hey i have a model that can be accesed through 2 different urls(depending on the domain). And i use them like this in views and templates without problems.

When building the sitemap the get_absolute_url should not return the same so i thought: i can subclass the model and override the get_absolute_url method:

class FanpitConcert(BandtasticConcert):
    def get_absolute_url(self):
        return ('event_checkout',(),{'artist_slug':self.slug_name,
                                     'year': self.get_date().year,
                                     'month': self.get_date().month,
                                     'day': self.get_date().day,
                                    })
    class Meta:
        abstract = True

And then use this subclassed model for the sitemap class

class ConcertsSiteMap(Sitemap):
    def items(self):
        return FanpitConcert.objects.all().filter(app='Fanpit')

But when i access /sitemap.xml django still calling the get_absolute_url from the original model

Is there any dark magic django is doing here? or Am i missing something obvious?

UPDATE

I tried removing the abstract = True part and went with class Meta: db_table = 'same_table_as_base_model'

But now django complains about not finding columns.


Solution

  • Instead of abstraction, it would be better to use proxy models in this case.

    So the subclassed models would have

    class Meta:
        proxy = True
    

    More info on proxy models here.