feincms / feincms3-example

Example feincms3 project providing a pages and an article app
https://feincms3.readthedocs.io
12 stars 6 forks source link

Dynamic way for category adding #2

Closed serdargkaya closed 7 years ago

serdargkaya commented 7 years ago

Hi matthiask

How can we use ForeignKey to automate adding categories.?

matthiask commented 7 years ago

Hey,

Just asking: Do you really want a dynamic category selection? This would probably also mean that you have to add a page for each category. If you only want to keep articles at a single place in the page hierarchy the pages and feincms3 do not have to know anything about the categories (and the example articles app in feincms3-example is already too complicated)

However, if you really require this, here are a few hopefully helpful snippets copied from the source of www.fcz.ch

app/articles/models.py:

from feincms3.apps import reverse_app

class Article(models.Model):
    # ...

    CATEGORIES = [
        ('club', _('club')),
        ('men', _('men')), 
        ('women', _('women')),
        # ...
        ('general', _('general')),
    ]

    category = models.CharField(
        _('category'),
        max_length=20,
        choices=CATEGORIES,
        db_index=True,
    )

    # ...

    def get_absolute_url(self):
        return reverse_app(
            ('articles-%s' % self.category, 'articles'),
            'article-detail',
            kwargs={'year': self.publication_date.year, 'slug': self.slug},
        )

app/pages/models.py:

class Page(AbstractPage, ...):
    # ...

    APPLICATIONS = [
        ('articles', _('articles'), {
            'urlconf': 'app.articles.urls',
            'app_instance_namespace': lambda page: page.articles_namespace,
            'required_fields': ('articles_namespace',),
        }),
        # ...
    ]

    ARTICLES_NAMESPACE_CHOICES = [
        ('articles-%s' % key, title) for key, title in Article.CATEGORIES
    ]
    ARTICLES_NAMESPACE_CHOICES = Team.ARTICLES_NAMESPACE_CHOICES + [
        ('articles', _('all articles')),
    ]
    articles_namespace = models.CharField(
        _('articles namespace'),
        max_length=50,
        choices=ARTICLES_NAMESPACE_CHOICES,
        blank=True,
    )

Of course that's not a foreign key yet, but hopefully you get the idea:

I hope this helps a bit; I'll happily explain more here, and/or hopefully add some examples to the documentation later.

serdargkaya commented 7 years ago

Thank you for the explanation. I'm building a news app. I need to make category creation automatic for clients. I don't wanna create pages for every category. Just wanna list items for every category thats all. Can u give more detail about how to do this? Im huge fan of cbv. Do you consider this for feincms3 ?

And thanks again

matthiask commented 7 years ago

Then I'd just create one single articles app, and keep categories and articles in there. You'll probably add a CategoryListView and an ArticleListView which filters by categories or something, maybe a ClientListView which links to the pre-filtered ArticleListView?

If you don't want to add pages then I wouldn't bother with app_instance_namespace and friends.

I'm not a big fan of CBV myself, but it does not matter too much because I don't intend to add views to feincms3. It's also not really necessary IMHO since views using feincms3 can be really short which makes it a bit pointless to add customization possibilities etc.

serdargkaya commented 7 years ago

Thank you for the quick reply. I already wrote the news application. I am thinking of using FeincM3 for other pages. But I could not integrate with category. Will try to come up with something.

matthiask commented 7 years ago

Btw, I noticed that feincms3-example was still using the PluginRenderer from django-content-editor. It now uses feincms3' TemplatePluginRenderer which makes the view code even simpler and shorter:

https://github.com/matthiask/feincms3-example/blob/master/app/pages/views.py