1. hideki nara
  2. mandb

Commits

hideki nara  committed 616abcd

search command

  • Participants
  • Parent commits 1dddd8e
  • Branches default

Comments (0)

Files changed (1)

File src/mandb/management/commands/db.py

View file
             dest='database',
             default='default',
             help=u'index for settings.DATABASES[database]'),
+
+        make_option('--w',
+            action='store',
+            dest='word',
+            default=None,
+            help=u'word for search'),
         
         make_option('--dir',
             action='store',
                 print r
         return ret 
          
+    def handle_search(self,*args,**options):
+        import os
+        from django.core import serializers
+        from django.db import models 
+
+        mcs = self.handle_model_list(console=False,*args,**options)
+        if len(mcs) < 1 or mcs[0] == None:
+            print "no models"
+            print "check --c=model_list --a=app.*"
+            return
+
+        for mc in mcs :
+            assert mc != None 
+            
+            q=models.Q()
+            for f in mc._meta.fields:
+                if all([ not isinstance(f,x) for x in [
+                        models.AutoField,models.ManyToManyField, models.ForeignKey ,
+                        models.DateTimeField,
+                        ] ]):
+                    if isinstance(f,models.IntegerField ) :
+                        if options['word'].isdigit() :
+                            q = q | models.Q(**{f.name : options['word']} )
+                        else:
+                            continue 
+                    else:
+                        q = q | models.Q(**{f.name+"__contains" : options['word']} )
+
+            objects = mc.objects.filter(q )
+
+            for obj in objects:
+                print mc._meta.object_name,obj.id,obj
+
 
     def handle_fixture(self,*args,**options):
         ''' 指定したモデルのfixtureを作る