Closed serdargkaya closed 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:
app_instance_namespace
which returns an instance namespace such as articles-club
, articles-men
etc., and you use the same instance namespace to reverse URLs in Article.get_absolute_url
. Of course, you should probably always keep the fallback articles
namespace around if you want articles to appear anyway somewhere.required_fields
, but that's up to you.I hope this helps a bit; I'll happily explain more here, and/or hopefully add some examples to the documentation later.
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
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.
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.
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
Hi matthiask
How can we use ForeignKey to automate adding categories.?