Link products to brands, categories and tags with object and nested fields, and build a category tree.
Welcome back. In this guide, you'll connect your models: products to brands, categories and tags.
There are two kinds of relation. An object field points to one record: each product has one brand. A nested field holds a list: a product can have many tags. First, let's add the models they point to. Click New Model.
Let's start with categories. Categories can have subcategories. For example, Running Shoes is a subcategory of Footwear. Same start as before: an id field. It's an integer, and it's the primary key. Next, a name field. Its type is text, for words people search for. Make it searchable, and make it required. Then a slug. It's a keyword: a URL-friendly version of the name, like footwear. Turn on Self-referencing. Not yet: a tree needs labels. Visible In The Related Model picks the fields that travel with a relation, so mark the name. Now it works. Three fields are added for you. Parent id is the only one you write: the parent category's id, or empty for a top level. Depth and ancestors are filled in for you. All three come back in every category response. Depth is the level in the tree. Ancestors is the path from the top, so you can show a breadcrumb without extra requests. Changed your mind? Untick the box. The three fields are removed. Tick it again, and they're back. This works until the model is locked. After that, the tree stays. Save and Continue.
Tags are simpler. Name the model tag, and start the same way: an id field, an integer, as the primary key. Then a name field. It's a keyword, since each tag is an exact value, like waterproof. Make it searchable and required. And mark it Visible In The Related Model, so a product can show its tag names. Save.
Now, back to the product. Add a field called brand, and pick object as its type. An object field always needs a model, so choose the one it points to: brand. Relations go one level deep: a brand's own relations don't come along. That's why object and nested fields have no Visible option, and no Searchable option either. Do the same for category. Category is the one exception: it also brings its ancestors, ready for a breadcrumb. And for tags, pick nested, with the tag model. So what does a product look like in your API? Take the brand: each product carries the brand's id, plus the fields you marked visible, like the brand name. And because the brand name is both searchable and visible, a full-text search on products for Northpeak finds every Northpeak product. Save. Here are the new fields.
So what happens to brand now? Since product points to it, its existing fields are protected. The same goes for category and tag. Let's try renaming country. It can't be changed, and neither can a field's type or the primary key. So, back it goes. Adding new fields is still fine, and so are flags like Required and Visible.
Let's leave brand as it is and head back to Models. All four models are now connected: each product points to a brand, a category and its tags. In the next guide, we'll create an endpoint and see the full shape of a product.
Read the details behind this guide on the documentation page.