简体   繁体   English

Django 标签模型设计

[英]Django Tag model design

I am wondering if the following is the correct way to create tagging system for images and being able to render a tag cloud:我想知道以下是否是为图像创建标记系统并能够呈现标记云的正确方法:

from django.db import models

class Tag(models.Model):
    word        = models.CharField(max_length=35)
    slug        = models.CharField(max_length=250)
    created_at  = models.DateTimeField(auto_now_add=False)

    def __unicode__(self):
        return self.word

class Photo(models.Model):
    slug                = models.CharField(max_length=250)
    filename            = models.CharField(max_length=200)
    extension           = models.CharField(max_length=4)
    size                = models.IntegerField()
    ...
    tags                = models.ManyToManyField(Tag)

    def __unicode__(self):
        return self.slug

Note that my database table will include millions of rows and each image will have 4-8 tags.请注意,我的数据库表将包含数百万行,每个图像将有 4-8 个标签。

Please advise.请指教。

If all you want to do is create a tag cloud, than that data model should be sufficient.如果您只想创建一个标签云,那么该数据模型就足够了。 I would make one modification:我会做一个修改:

tags = models.ManyToManyField(Tag,related_name='photos')

That will make reverse lookups in you photo views cleaner to read and easier to remember.这将使您的照片视图中的反向查找更易于阅读和记忆。

However, I would consider other use cases for your tags.但是,我会考虑您的标签的其他用例。 Is a tag cloud the only thing you want to use the tagging for?标签云是您唯一想要使用标签的吗? Is there any meta data that the relationship should contain?是否有关系应该包含​​的元数据?

If you're planning on having millions of rows, then caching is going to be as important as the data model.如果您计划拥有数百万行,那么缓存将与数据模型一样重要。

Also, to avoid reinventing the wheel, see if anyone else has built a library that serves your purposes: http://www.djangopackages.com/grids/g/tagging/另外,为了避免重新发明轮子,看看是否有其他人已经构建了一个满足您目的的库: http : //www.djangopackages.com/grids/g/tagging/

Handling tags youself could be a hard job.自己处理标签可能是一项艰巨的工作。 You can easily use django library.您可以轻松使用 django 库。 Install it with pip用pip安装

pip install django-taggit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM