Задать вопрос

Тел: +7 965 3737 888





Make tags easier with properties

<p>Jonathan Buchanan's <a href="http://code.google.com/p/django-tagging/">Django tagging</a> application is the best thing since sliced bread, and makes adding generic tagging to any model trivially easy. But you can make it just a tiny bit easier to use by setting up a property on your model for handling the tags.</p>
<p>Once you've set this up, you can access and set tags in a fairly natural way:</p>
&gt;&gt;&gt; obj = MyModel.objects.get(pk=1)
&gt;&gt;&gt; obj.tags = 'foo bar'
&gt;&gt;&gt; obj.tags
[&lt;Tag: foo&gt;, &lt;Tag: bar&gt;]

<p>And remember that obj.tags will return a QuerySet, so you can do filtering on it.</p>

Вопрос полезен? Да0/Нет0

Ответы (4):

Ответ полезен? Да0/Нет0

good job i'm newbie in python, thinks for introduce property to me.

Ответ полезен? Да0/Нет0

It's worth noting that Jonathan has now added a TagField to django-tagging, which uses a descriptor to get/set the tags. That offers a similar interface to this, but wraps it up with admin-interface goodness :)

Ответ полезен? Да0/Нет0

We actually do something similar to this as well, it's quite handy. The only difference is for our get tags and set tags methods we validate the data to make sure we can't simply skip a query.

Ответ полезен? Да0/Нет0

Clever clever :)

There's likely an easy way to encapsulate this into a descriptor so you could just do:

tags = TagField()

and have everything work like you said. I should experiment with that...