title: Django: Creating categories using querysets/filtering
body: <p>I'm trying to figure out if it's possible to create categories using custom filters.</p>
  
  <p>I am building an e-commerce app and I have set up my category model using mptt. I am importing a csv that creates my top level categories which works fine. The problem is I will need to have sub-categories that are more specific e.g Men's Clothing(Top Level) > Jeans.</p>
  
  <p>The csv has several fields that contains info relating to each product e.g description: "stone wash bootcut jeans". I would ideally like to check these fields for keywords and add each product to the correct categories. Is it possible to set up categories this way or is there an alternative solution?</p>
  
  <p>I am a django newbie so any help is appreciated.</p>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>from django.db import models
  from mptt.models import MPTTModel, TreeForeignKey
  
  class Category(MPTTModel):
      name = models.CharField(max_length=50, unique=True)
      parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True, on_delete=models.CASCADE)
      slug = models.SlugField()
  
      class MPTTMeta:
          order_insertion_by = ['name']
  
      class Meta:
          unique_together = (('parent', 'slug',))
          verbose_name_plural = 'categories'
  
      def get_slug_list(self):
          try:
              ancestors = self.get_ancestors(include_self=True)
          except:
              ancestors = []
          else:
              ancestors = [ i.slug for i in ancestors]
          slugs = []
          for i in range(len(ancestors)):
              slugs.append('/'.join(ancestors[:i+1]))
          return slugs
  
  
      def __str__(self):
          return self.name
  
  class Brands(models.Model):
      brand_name = models.CharField(max_length=500, default='')
  
      def __str__(self):
          return self.brand_name
  
  
  class Product(models.Model):
      aw_deep_link = models.CharField(max_length=500, default='')
      description = models.CharField(max_length=500, default='')
      product_name = models.CharField(max_length=500, default='')
      aw_image_url = models.CharField(max_length=500, default='')
      search_price = models.DecimalField(max_digits=6, decimal_places=2, null=True)
      merchant_name = models.CharField(max_length=500, default='')
      display_price = models.CharField(max_length=500, default='')
      brand_name = TreeForeignKey('Brands', on_delete=models.CASCADE)
      colour = models.CharField(max_length=500, default='')
      rrp_price = models.DecimalField(max_digits=6, decimal_places=2, null=True)
      category = TreeForeignKey('Category',null=True,blank=True, on_delete=models.CASCADE)
      slug = models.SlugField(default='')
  
      def __str__(self):
          return self.product_name
  </code></pre>
  
  <p><strong>importCSV.py</strong></p>
  
  <pre><code>import re
  from products.models import Category, Brands
  from django.core.management.base import BaseCommand
  
  
  class Command(BaseCommand):
      help = "Load some sample data into the db"
  
      def add_arguments(self, parser):
          parser.add_argument('--file', dest='file', help='File to load')
  
      def handle(self, **options):
          from products.models import Product
  
          if options['file']:
              print("Importing " + options['file'])
  
              with open(options['file']) as f:
                  linecount = 0
                  next(f)
                  for line in f:
                      linecount += 1
                      fields = line.split(',')
                      category = Category.objects.get_or_create(name=fields[10])
                      brand_name = Brands.objects.get_or_create(brand_name=fields[7])
  
                      data = {
                              'aw_deep_link':  fields[0],
                              'description': fields[1],
                              'product_name': fields[2],
                              'aw_image_url':  fields[3],
                              'search_price':  fields[4],
                              'merchant_name': fields[5],
                              'display_price':  fields[6],
                              'brand_name':  brand_name[0],
                              'colour' :  fields[8],
                              'rrp_price' :  fields[9],
                              'category' :  category[0],
  
                      }
  
                      product = Product(**data)
                      product.save()
  
                  print("Added {0} products".format(linecount))
  </code></pre>