Book Image

Learning Website Development with Django

Book Image

Learning Website Development with Django

Overview of this book

Table of Contents (18 chapters)
Learning Website Development with Django
Credits
About the Author
About the Reviewers
Preface
Index

The Tag Data Model


Tags need to be stored in the database and associated with bookmarks. So the first step in introducing tags to our project is creating a data model for tags. A tag object will only hold one piece of data, a string that represents the tag. In addition, we need to maintain the list of tags associated with a particular bookmark. You may recall from chapter 3 that we used foreign keys to associate bookmarks with users, and we called this a one-to-many relationship. However, the relationship between tags and bookmarks is not one-to-many, because one tag can be associated with many bookmarks, and one bookmark can also have many tags associated with it. This is called a many-to-many relationship, and it is represented in Django models using models.ManyToManyField.

You should be well aware by now that data models go into bookmarks/models.py, so open the file and add the following Tag class to it:

class Tag(models.Model):
  name = models.CharField(maxlength=64, unique=True)
  bookmarks...