0

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
Frank
  • 1,959
  • 12
  • 27
  • it doesn't seem so. In dashboard.pages.views, they do `FlatPage = get_model('flatpages', 'FlatPage')`. One idea the could work would be cloning/forking `django.contrib.flatpages` to a new app, call it "flatpages" too, and replace `django.contrib.flatpages` in INSTALLED_APPS. But even if this works you have things like the fields to use in the Create/Update form inside a class of oscar dashboard pages app. – daigorocub Nov 16 '21 at 15:50

1 Answers1

0

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.

Frank
  • 1,959
  • 12
  • 27