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

Тел: +7 965 3737 888

464

Просмотров

4

Ответов

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
file_4005.py(504.0б)
None

Ответы (4):

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

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

Ответubernostrum:25.08.2007
Ответ полезен? Да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 :)

Ответzeeg:06.03.2007
Ответ полезен? Да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.

Ответjacobian:06.03.2007
Ответ полезен? Да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...