How to customize flatpages to use with django-oscar get_model

282 Views Asked by At

Unfortunately, after countless attempts, I haven't found any promising way to solution.

I'm searching for the least intrusive way to customize the flatpages used by django-oscar's 'dashboard.pages' app. It would be ok to break the migration history of the django app and manage them manually.

Is it possible to solve it using the get_model loader from oscar and how? (I don't need a full solution and am thankful for every hint)


Additional information:

My intention is to create a FlatPageCategory model

from oscar.core.utils import slugify

class FlatPageCategory(models.Model):
    name = models.CharField(max_length=255, db_index=True)
    url = models.SlugField()

and add a few fields to FlatPage model

from django.contrib.flatpages.models import FlatPage

class FlatPage(FlatPage):
    display_order = models.SmallIntegerField(
        default=0,
    )
    category = models.ForeignKey(
        'FlatPageCategory',
        on_delete=models.SET_NULL,
        blank=True, null=True,
    )
    attachments = models.ManyToManyField(
        File,
    )

    def get_absolute_url(self):
        """ Prepend category url if category exists. """
        url = super().get_absolute_url()
        if self.category:
            return f'{self.category.url}/{url}'
        return url
1

There are 1 best solutions below

0
On

After trying different approaches this was my solution: Creating OneToOne-Relationship from a custom page model to FlatPage and a ForeignKey from Attachment to FlatPage.

class Attachment(models.Model):
    flatpage = models.ForeignKey(
        FlatPage,
        related_name='attachments',
        on_delete=models.CASCADE,
    )
    title = models.CharField(
        max_length=250,
    )
    file = models.FileField(
        _('File'),
        upload_to='page_attachments/'
    )


class Page(models.Model):
    """ This is passed to the template context to build menu and urls """
    flatpage = models.OneToOneField(
        FlatPage,
        related_name='page',
        on_delete=models.CASCADE,
    )
    category = models.PositiveSmallIntegerField(
        _('Category'),
        choices=Category.choices,
        default=Category.UNDEFINED,
    )
    priority = models.SmallIntegerField(
        _('Priority'),
        default=0,
    )

You can find the source of this implementation in the GitHub-Project.

It is not really the solution I wanted and asked in the question and therefore I think the question should stay open.