1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from django.db import models
from tagging.models import Tag
class MyModel(models.Model):
name = models.CharField(maxlength=250)
tag_list = models.CharField(maxlength=250)
def save(self):
super(MyModel, self).save()
self.tags = self.tag_list
def _get_tags(self):
return Tag.objects.get_for_object(self)
def _set_tags(self, tag_list):
Tag.objects.update_tags(self, tag_list)
tags = property(_get_tags, _set_tags)
|
More like this
- Repeat blocks with new context / simple Jinja-like macro system by miracle2k 5 years, 10 months ago
- Scoped Cache Compatible with Django Caching Helpers by axiak 5 years, 2 months ago
- Flickr Sync by bretwalker 5 years, 10 months ago
- CharField powered Tags with ChoiceField widget. by Husio 3 years, 9 months ago
- Template tag to sort a list of links by pytechd 5 years, 9 months ago
Comments
Clever clever :)
There's likely an easy way to encapsulate this into a descriptor so you could just do:
and have everything work like you said. I should experiment with that...
#
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.
#
It's worth noting that Jonathan has now added a
TagFieldto 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 :)#
good job i'm newbie in python, thinks for introduce property to me.
#