Add the command 'manage.py convertcats2tags' to convert existing categories and subcategories to tags.

This commit is contained in:
Fred Pauchet 2012-09-03 11:42:20 +02:00
parent 36fa059237
commit ffae1d63db
3 changed files with 28 additions and 2 deletions

View File

@ -0,0 +1,26 @@
#coding=utf-8
from django.core.management.base import BaseCommand, CommandError
from django.core.files import File
from xcards.cards.models import *
import os
from lxml import etree
class Command(BaseCommand):
help = 'Import data from an xml file generated by gc-star'
def handle(self, *args, **options):
for item in Item.objects.all():
print item.subcategory
t1, created1 = Tag.objects.get_or_create(label=str(item.subcategory))
t2, created2 = Tag.objects.get_or_create(label=str(item.subcategory.category))
t1.save()
t2.save()
# add tags to item
item.tags.add(t1)
item.tags.add(t2)
# save item
item.save()

View File

@ -39,7 +39,7 @@ class SubCategory(models.Model):
ordering = ['label']
def __unicode__(self):
return self.category.label + ' - ' + self.label
return self.label
class Tag(models.Model):
label = models.CharField(verbose_name='Label', max_length=50)
@ -50,7 +50,6 @@ class Tag(models.Model):
return self.label
class Item(models.Model):
country = models.ForeignKey(Country, verbose_name='Pays')
subcategory = models.ForeignKey(SubCategory, verbose_name='Thème')
label = models.CharField(verbose_name='Titre', max_length=50)

View File

@ -44,6 +44,7 @@ def query(request):
if request.method == "POST":
terms = request.POST['q']
request.POST.urlencode()
else:
terms = request.GET['q']