开发者

Categories in blog. How do they work?

I just do not know how to create model for categories. I've got some users which can post messages for one or many groups. There will be one category for each message, so visiters will be able to filter all开发者_运维百科 messages for this category. I thought about such realisations:

  • One table for all categories, for all users. I'll have in admin panel autocomplete widget that will check for available category and if there no correct categories user will add them through "plus" icon or, maybe, with redefinition of save method I'll add new category to database.
  • Categories - just CharField in model. So I'll get all categories through split() method and somehow put it on my webpage.

Maybe there other ways to create categories for messages? Something, which can separate categories for different users.


Create a model called Category that will hold all of your categories, then create a ForiegnKey relationship from Message to Category to hold the category for that message, and a ManyToMany relationship from UserProfile to Category to hold all of the categories for a certain user.

class Category(models.Model):
    name = models.CharField(max_length=30)

class Message(models.Model):
    ...
    category = models.ForeignKey(Category)

class UserProfile(models.Model):
    ...
    categories = models.ManyToManyField(Category)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜