Lookaway App Profile Models

Documented by Kyle Bruder on Apr 05, 2021
Last updated on Jun 05, 2021


logo

Introduction

In this example we will cover the Lookaway Posts app to demonstrate how to inherit the Lookaway App Profile mixins and build on them to suit your needs. AppProfile contains basic settings such as the display name of the app. Section allows page sections to be added to the landing page.


  1. Import the App Profile Model Mixins

    Add the following line near the top of your app's 'models.py' file.

    posts/models.py
    ...
    from django.db import models
    from crypto.models import CryptoWalletsMixin
    from lookaway.mixins import AppProfile, Section
    ...
    

    Source: kbruder Tech

  2. Add an App Profile Model to Your App

    Here we have inherited the AppProfile model mixin and the CryptoWallets mixin and added some additional settings that can be updated to change how items appear on the landing page and list views. Add an AppProfile model to your app's models.py file.

    posts/models.py
    class PostsAppProfile(AppProfile, CryptoWalletsMixin):
    
        title = models.CharField(
            max_length=255,
            default="Posts"
        )
    
        n_posts = models.PositiveIntegerField(default=25)
        n_responses = models.PositiveIntegerField(default=25)
        post_list_pagination = models.PositiveIntegerField(default=10)
        response_list_pagination = models.PositiveIntegerField(default=10)
        show_new_posts = models.BooleanField(default=True)
        show_top_posts = models.BooleanField(default=True)
        show_new_responses = models.BooleanField(default=True)
        show_top_responses = models.BooleanField(default=True)
        logo = models.ForeignKey(
            'objects.image',
            on_delete=models.SET_NULL,
            blank=True,
            null=True,
            related_name='posts_logo'
        )
        banner = models.ForeignKey(
            'objects.image',
            on_delete=models.SET_NULL,
            blank=True,
            null=True,
            related_name='posts_banner'
        )
        bg_image = models.ForeignKey(
            'objects.image',
            on_delete=models.SET_NULL,
            blank=True,
            null=True,
            related_name='posts_bg_image'
        )
    

    Source: kbruder Tech

  3. Add a Page Section Model

    Here we have inherited the Section mixin and added some fields in case we want to add alerts to the landing page. Add the model in your app's models.py file.

    posts/models.py
    class PostsPageSection(Section):
    
        info = models.TextField(
            max_length=65535,
            blank=True,
            null=True,
        )
        alert = models.TextField(
            max_length=65535,
            blank=True,
            null=True,
        )
        posts = models.ManyToManyField(
            'posts.post',
            blank=True,
        )
        is_enabled = models.BooleanField(default=False)
    
        class Meta:
            verbose_name = "Landing Page Section"
            verbose_name_plural = "Landing Page Sections"
            ordering = ['order']
    

    Source: kbruder Tech

  4. Add the New Models to the Admin

    If you want to be able to access the new models in the Django Admin app, remember to register them first. Even if you plan on adding forms for these models (which is highly recommended) it is convenient to be able to use the Admin when troubleshooting code.

    posts/admin.py
    rom django.contrib import admin
    from .models import PostsAppProfile, PostsPageSection, Post, ResponsePost
    
    # Register your models here.
    
    admin.site.register(PostsAppProfile)
    admin.site.register(PostsPageSection)
    
    ...
    

  5. Migrate the Database

    Whenever an apps models have been changed or new models have been added, you must migrate the database.

    $ python manage.py makemigrations posts
    $ python manage.py migrate posts
    

    Replace 'foo' with the name of the app.

Referenced by

App Profile Design Patterns - Lookaway CMS

Lookaway CMS comes with mixins, models, forms, and views that can be used when writing new Django apps.

bitcoin:3MnhNRKgrpTFQWstYicjF6GebY7u7dap4u
Bitcoin Accepted Here

3MnhNRKgrpTFQWstYicjF6GebY7u7dap4u

Please donate some Bitcoin to our site. We can use it to keep improving the site and open up to more members. Any amount will help. Thank you.


litecoin:MT61gm6pdp9rJoLyMWW5A1hnUpxAERnxqg
Litecoin Accepted Here

MT61gm6pdp9rJoLyMWW5A1hnUpxAERnxqg

Please donate some Lite to our site. We can use it to keep improving the site and open it up to more members. Any amount will help. Thank you.