有人告诉我这没有意义,因为管理器对所有行(而不是单个实例)进行操作,但是我在django-taggit库中看到了要实现的目标。
此处:https://github.com/alex/django-taggit/blob/master/taggit/managers.py
安装过程如下:
<div>
<label for="item">Item</label>
<select id="item">
<option value="">--- Select an Item ---</option>
<option value="Sofa">Sofa</option>
<option value="Chair">Chair</option>
</select>
</div>
<div>
<label for="type">Type</label>
<select id="type"></select>
</div>
<div>
<label for="color">Color</label>
<select id="color"></select>
</div>
<script>
var types = [];
var colors = [];
// Get a reference to the select boxs
var item = document.getElementById('item');
var type = document.getElementById('type');
var color = document.getElementById('color');
// Add an event handler for when an item is selected
item.addEventListener('change', function () {
i = item.options[item.selectedIndex].value;
if (i == "Sofa") {
types = [
{ text: "Full Size", value: "full-size" },
{ text: "Love Seat", value: "love-seat" }
];
}
if (i == "Chair") {
types = [
{ text: "Standard", value: "standard" },
{ text: "Recliner", value: "recliner" }
];
}
setOptions(type, types);
});
// Add an event handler for when an type is selected
type.addEventListener('change', function () {
i = type.options[type.selectedIndex].value;
console.log(type.options[type.selectedIndex].value);
if (i == "full-size") {
colors = [
{ text: "Brown", value: "brown" },
{ text: "White", value: "white" },
{ text: "Black", value: "black" }
];
}
if (i == "love-seat") {
colors = [
{ text: "Tan", value: "tan" },
{ text: "Red", value: "red" },
{ text: "Copper", value: "copper" }
];
}
if (i == "standard") {
colors = [
{ text: "Green", value: "green" },
{ text: "Yellow", value: "yellow" },
{ text: "Red", value: "red" }
];
}
if (i == "recliner") {
colors = [
{ text: "Orange", value: "orange" },
{ text: "Purple", value: "purple" },
{ text: "Magenta", value: "magenta" }
];
}
setOptions(color, colors);
});
// Function to add Options
function setOptions(select, opts) {
// Clear existing values
select.innerHTML = "";
// Add a select option
var elm = document.createElement('option');
elm.appendChild(document.createTextNode("--- Select an Option"));
select.appendChild(elm);
for (o in opts) {
var opt = opts[o];
// create new option element
var elm = document.createElement('option');
// create text node to add to option element (opt)
elm.appendChild(document.createTextNode(opt.text));
// set value property of opt
elm.value = opt.value;
// add opt to end of select box (sel)
select.appendChild(elm);
}
}
</script>
然后标记任何内容,只需执行以下操作即可
:from django.db import models
from taggit.managers import TaggableManager
class Food(models.Model):
# ... fields here
tags = TaggableManager()
注意:apple.tags.add("red", "green", "fruit")
不是apple
。
但是,当我自己尝试做时,我得到:Apple
!
我的代码:
AttributeError: Manager isn't accessible via MyModelName instances
然后,我尝试按以下方式调用add方法:
from django.db import models
class TagManager(models.Manager):
def add(self, *tags):
print("Testing...")
class List(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=500)
tags = TagManager()
我如何使其工作?