Generate Model Data. Lots of Options.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import random,string,datetime,os,re,time
from decimal import Decimal
from django.core.exceptions import ValidationError
from optparse import make_option
from django.contrib.webdesign.lorem_ipsum import words,paragraphs
from django.core.management.base import BaseCommand
from django.db.models import get_app,get_models,URLField
from django.conf import settings
import MySQLdb

#authors:
#adam rutkowski <adam@mtod.org>
#aaron smith <aaron@macendeavor.com>

"""
EXAMPLE
models.py:

class DillaController():
	#(Optional) this defines the order of how models are populated. (Fixes cross ForeignKey problems)
	models=('UserProfile','Venue','Event','Genre','Artist')

class Event(models.Model):
	venue=models.ForeignKey('Venue')
	artists=models.ManyToManyField('Artist')
	date=models.DateTimeField(blank=False,null=False)
	showtime=models.TimeField(blank=False,null=False)
	doortime=models.TimeField(blank=False,null=False)
	covercharge=models.DecimalField(decimal_places=2,max_digits=7)
	created_at=antaresia_models.ModelCommons.created_at()
	updated_on=antaresia_models.ModelCommons.updated_on()
	#usual Dilla meta class
	class Dilla():
		skip_model=False
		generate_images=False
		image_fields=None
		resolution="1024x768" #if images, this resolution
		resolutions=('300x340','200x190','100x10','200x90') #if images, use a random resolution from this tuple
		field_extras={ #field extras are for defining custom dilla behavior per field
			'fieldname':{
				'generator':None, #can point to a callable, which must return the desired value. If this is a string, it looks for a method in the dilla.py file.
				'generator_wants_extras':False, #whether or not to pass this "field extra" hash item to the callable
				'random_values':("word","yes","no","1"), #choose a random value from this tuple
				'resolution':'1024x768', #if images, use this image size for this field
				'resolutions':('300x340','200x190','100x10','200x90'), #if images, use a random size from this tuple
				'max':10, #for many to many fields, the maximum associated objects will be 10, so it will take a range like: Model.objects.all().order_by("?")[0:random.randrange(0,max)]
				'spaces':False, #if Char/TextField, whether or not to allow spaces
				'word_count':1, #if Char/TextField, the number of words to generate
				'word_range':(3,7), #a range of words to generate (3-7 words)
				'paragraph_count:1, #if TextField, the number of paragraphs to generate
				'paragraph_range':(3,5), #a range of paragraphs to generate (3 - 5 paragraphs)
				'integer_range':(0,2), #a range for any integer type field (IntegerField,SmallIntegerField,PositiveInteger,PositiveSmallInteger)
				#TODO:'digits_only':True, #if Char/TextField, but want numbers only
				#TODO:'digit_range:(30,400), #a range for digit only creation (default range is (0,9999))
				#TODO:'digit_ranges':((0,20),(30,300)), #chooses random range from this tuple.
				#TODO:'digits_for_words':False|True (Default:False) #generates lipsum words, but then replaces each word with numbers. (like a sentance of numbers)
			},
			'anotherfieldname':{...}
		}

"""

image_support=False
try:
	import Image,ImageDraw,ImageFont
	ROOT=os.path.split(__file__)[0]+"/../"
	FAKE_UPLOAD_RELATIVE="dilla-fakes/"
	FAKE_UPLOAD_PATH="%s%s"%(settings.MEDIA_ROOT,FAKE_UPLOAD_RELATIVE)
	#armn(Apple Roman),ADBE(Adobe Expert),ADOB(Adobe Standard),symb(Microsoft Symbol),unic(Unicode),armn(TTF_ENCODING)
	TTF_ENCODING=getattr(settings,'TTF_ENCODING','armn')
	if not os.path.exists(FAKE_UPLOAD_PATH):os.makedirs(FAKE_UPLOAD_PATH)
	fonts=['Besmellah_1.ttf','skullz.ttf','bonohadavision.ttf','openlogos.ttf', 'invaders.from.space.[fontvir.us].ttf','anim____.ttf']
	image_support=True
except ImportError, e:
	print 'Images not supported, something went wrong: %s' % e
    
confirm_message="""Are you sure you want to run Dilla? 
It will add a lot of random data to your database %s@%s.
Type 'yes' to confirm.
"""%(settings.DATABASE_USER,settings.DATABASE_NAME)

class Command(BaseCommand):
	"""
	Dilla is a command that populates your database with randomized data. (http://gitweb.codeendeavor.com/?p=dilla.git;a=summary)

	Examples:
	1. Generate data for all models in an app
	   >>python manage.py dilla app_name
	
	2. Generate data for all models in all apps listed
	   >>python manage.py dilla app_name app_name
	
	3. Generate data for all models in all apps listed (an alternative to #2)
	   >>python manage.py dilla -a app_name -a app_name
	
	4. Generate data for supplied models, in the supplied apps
	   >>python manage.py dilla -a app_name -m ModelName
	
	5. Addition to #4, generate data for multiple models
	   >>python manage.py dilla -a app_name -m ModelName -m AnotherModelName
	
	** The order of app names and model names are important, if a model has
	   a ForeignKey to another model, but there isn't data available yet
	   in the foregn table, problems occur.
	
	** When using -a or -m, you don't need to use full python path.
	"""
	requires_model_validation=True
	output_transaction=True
	help=__doc__
	args='appname [appname ...]'
	option_list=BaseCommand.option_list+(
		make_option('--iter','-i',default='20',action='store',dest='iterations',help='Number of iterations per model. Default is 20.'),
		make_option('--no-doubt','-n',action='store_true',dest='no_doubt',help='Always fill fields that can be blank. Do not randomly decide.'),
		make_option('--app','-a',action='append',dest='apps',help='Generate data for these apps.'),
		make_option('--model','-m',action='append',dest='models',help='Generate data for these models.'),
	)
	
	def handle(self,*app_labels,**options):
		"""
		Main execution point
		"""
		if not settings.DEBUG:
			confirm=raw_input(confirm_message)
			if confirm != 'yes': return
		models=[]
		model_labels=[]
		apps=[]
		if app_labels and len(app_labels)>0: apps.extend(app_labels)
		if options.get("apps",False): apps.extend(options.get("apps"))
		if options.get("models",False): model_labels.extend(options.get("models"))
		for a in apps:
			app_label=a
			app=get_app(a)
			if len(model_labels)>0:
				app_models=get_models(app)
				for app_model in app_models:
					meta=app_model._meta
					for model_label in model_labels:
						if meta.object_name==model_label and meta.app_label==app_label:
							models.append(app_model)
			elif hasattr(app,"DillaController"):
				if hasattr(app.DillaController,"models"):
					for model in app.DillaController.models:
						if hasattr(app,model): models.append(getattr(app,model))
						else: print "Model " + model + " not found."
			else:
				models.extend(get_models(app))
		instances_by_model={}
		for model in models:
			dilla=None
			if not instances_by_model.get(model,None):instances_by_model[model]=[]
			if hasattr(model,'Dilla'):dilla=model.Dilla
			if dilla and getattr(dilla,'skip_model',False):continue
			for i in range(int(options['iterations'])):
				instance=model()
				for field in model._meta.fields:
					if not field.auto_created:
						if not field.blank or options['no_doubt'] or hasattr(field,"auto_now") or hasattr(field,"auto_now_add"):
							self.fill(field=field,obj=instance,dilla=dilla)
						elif field.blank:
							self._decide(self.fill,field=field,obj=instance,dilla=dilla)
				try:
					instance.save()
					#if field has unique, this error will be thrown, in the case of dilla, we don't care
				except MySQLdb.IntegrityError:
					instance=None
					continue
				instances_by_model[model].append(instance)
		for model in models: #go back through each model, and alter each instance's many to many fields
			if len(model._meta.many_to_many) <= 0: continue
			instances_list=instances_by_model[model]
			dilla=getattr(model,'Dilla',False)
			self.many_to_manys(model,instances_list,dilla)

	def _get_field_option(self,field_extras,option_name,default):
		"""
		Shortcut to get a field option
		self._get_field_option(field_option,"spaces",True)
		"""
		if not field_extras: return default
		return field_extras.get(option_name,default)

	def hashkey(self,**kwargs):
		"""
		Gererates an md5 hashkey. Use this with the 'generator' key, EX:
		field_extras={
			'myfield':{
				'generator':'hashkey'
			}
		}
		"""
		import md5
		m=md5.new()
		m.update(str(time.clock()))
		m.update(str(random.random()))
		m.update(str(random.random()))
		m.update(settings.SECRET_KEY)
		return m.hexdigest()

	def uuid(self,**kwargs):
		"""
		Generates a uuid, EX:
		field_extras={
			'myfield':{
				'generator':'uuid'
			}
		}
		"""
		import uuid
		return str(uuid.uuid1())

	def extended_zip(self,**kwargs):
		"""
		Generates an extended zip code (94109-4382) EX:
		field_extras={
			'myfield':{
				'generator':'extended_zip'
			}
		}
		"""
		int1=random.randint(11111,99999)
		int2=random.randint(1111,9999)
		return str(int1)+"-"+str(int2)

	def zip(self,**kwargs):
		"""
		Generates a zip code (94109) EX:
		field_extras={
			'myfield':{
				'generator':'zip'
			}
		}
		"""
		int1=random.randint(11111,99999)
		return str(int1)


	def many_to_manys(self,model,instances,dilla=None):
		"""
		Creates data for many to many fields
		"""
		many_to_manys=model._meta.many_to_many
		for instance in instances:
			for many_to_many_field in many_to_manys:
				max=5
				name=many_to_many_field.name
				if dilla and hasattr(dilla,"field_extras"):
					field_extras=dilla.field_extras.get(name,None)
					if field_extras: max=field_extras.get("max",5)
				ml=many_to_many_field.rel.to
				objects=ml.objects.all().order_by("?")
				count=objects.count()
				end=random.randrange(0,max)
				if count<end:relobjs=objects[0:count]
				else: relobjs=objects[0:end]
				setattr(instance,name,relobjs)
				instance.save
	
	def generate_PositiveIntegerField(self,**kwargs):
		"""
		Generates a PositiveIntegerField value.
		Supported field extras:
		field_extras={
			'myfield':{
				'integer_range':(0,10) #specify the integer range to generate
			}
		}
		"""
		field_extras=kwargs.get("field_extras",False)
		ranj=self._get_field_option(field_extras,"integer_range",(0,32))
		if len(ranj)<2:ranj=(0,32)
		if ranj[0]<0 or ranj[1]<0:
			print "PositiveInteger ranges cannot be less than zero, defaulting to range(0,32)"
			ranj=(0,32)
		return random.randint(ranj[0],ranj[1])
	
	def generate_PositiveSmallIntegerField(self,**kwargs):
		"""
		Generates a PositiveSmallIntegerField value.
		Supported field extras:
		field_extras={
			'myfield':{
				'integer_range':(0,10) #specify the integer range to generate
			}
		}
		"""
		field_extras=kwargs.get("field_extras",False)
		ranj=self._get_field_option(field_extras,"integer_range",(0,32))
		if len(ranj)<2:ranj=(0,32)
		if ranj[0]<0 or ranj[1]<0:
			print "PositiveSmallInteger ranges cannot be less than zero, defaulting to range(0,32)"
			ranj=(0,32)
		return random.randint(ranj[0],ranj[1])
	
	def generate_SmallIntegerField(self,**kwargs):
		"""
		Generates a SmallIntegerField value.
		Supported field extras:
		field_extras={
			'myfield':{
				'integer_range':(0,10) #specify the integer range to generate
			}
		}
		"""
		field_extras=kwargs.get("field_extras",False)
		ranj=self._get_field_option(field_extras,"integer_range",(0,32))
		if len(ranj)<2:ranj=(0,32)
		return random.randint(ranj[0],ranj[1])
	
	def generate_URLField(self,**kwargs):
		"""
		Returns a random URL for URLFields. By default there's only a few urls,
		but you can specify your own list of urls to choose from in settings.
		settings.DILLA_URLS=('http://www.google.com/','http://www.whitehouse.com',)
		"""
		urls=(
			"http://www.google.com/","http://www.amazon.com/","http://www.digg.com/",
			"http://www.nba.com/","http://www.espn.com/","http://www.python.org/",
		)
		urls=getattr(settings,"DILLA_URLS",urls)
		return urls[random.randrange(0,len(urls))]
	
	def generate_IPAddressField(self,**kwargs):
		"""
		Generates a random IP Address
		"""
		ip=str(random.randrange(0,255))+"."+str(random.randrange(0,255))+"."+str(random.randrange(0,255))+"."+str(random.randrange(0,255))
		return ip
		
	def generate_CharField(self,**kwargs):
		"""
		Generates char data for any CharField.
		Supported field extras:
		field_extras={
			'myfield':{
				'spaces':False|True, #(Default: True) #whether or not to allow spaces
				'word_count':3, #if specified, only 3 words will be generatd, if not specified, random between 1 and 4.
				'word_range:(2,5), #if specified, overrides the 'word_count' option, and will generate 2-5 random words.
			}
		}
		"""
		salt=""
		field_extras=kwargs.get("field_extras",False)
		if kwargs.get('unique',False): salt="".join([random.choice(string.digits) for i in range(random.randint(1,16))])
		word_count=self._get_field_option(field_extras,'word_count',-1)
		word_range=self._get_field_option(field_extras,'word_range',-1)
		if isinstance(word_range,tuple) and len(word_range)>1:
			result="%s %s" % (words(random.randint(word_range[0],word_range[1]),common=False),salt)
		elif word_count > 0:
			result="%s %s" % (words(word_count,common=False),salt)
		else:
			result="%s %s" % (words(random.randint(1,4),common=False),salt)
		max_length=kwargs.get('max_length',None)
		length=len(result)
		if max_length and length > max_length: result=result[length-max_length:] #chop off too many chars for max length
		if not self._get_field_option(field_extras,"spaces",True) and word_count == -1 and word_range == -1:
			result=result.replace(" ","")
		result=re.sub(r' $','',result)
		return result
	
	def generate_TextField(self,**kwargs):
		"""
		Generates text data for any TextField.
		Supported field extras:
		field_extras={
			'myfield':{
				'spaces':False|True, #(Default: True) #whether or not to allow spaces
				'paragraph_count':3, #The number of paragraphs to generate.
				'paragraph_range':(2,8) #A range for the number of paragraphs to generate - random between this range.
			}
		}
		"""
		field_extras=kwargs.get("field_extras",False)
		paragraph_count=self._get_field_option(field_extras,'paragraph_count',-1)
		paragraph_range=self._get_field_option(field_extras,'paragraph_range',-1)
		if isinstance(paragraph_range,tuple) and len(paragraph_range)>1:
			result="\n".join(paragraphs(random.randint(paragraph_range[0],paragraph_range[1])))
		elif paragraph_count > 0:
			result="\n".join(paragraphs(paragraph_count))
		else:
			result="\n".join(paragraphs(random.randint(1,3)))
		if not self._get_field_option(field_extras,'spaces',True): result=result.resplace(" ","")
		result=re.sub(r' $','',result)
		return result
	
	def generate_DecimalField(self,**kwargs):
		"""
		Generates a random decimal number
		"""
		return Decimal(str(random.random()+random.randint(1,20)))
	
	def generate_IntegerField(self,**kwargs):
		"""
		Generates a random integer
		"""
		return random.randint(1,255)
	
	def generate_TimeField(self,**kwargs):
		"""
		Generates time object for TimeField's
		"""
		today=datetime.datetime.now()
		return datetime.time(today.hour,today.minute,today.second)
	
	def generate_DateTimeField(self,**kwargs):
		"""
		Generates datetime for DateTimeField's
		"""
		return datetime.datetime.now()  
	
	def generate_DateField(self,**kwargs):
		"""
		Generates datetime for DateField's
		"""
		return datetime.datetime.now()
	
	def generate_ForeignKey(self, **kwargs):
		"""
		Finds a random foreign related object.
		"""
		field=kwargs.get('field',None)
		if not field: return None
		kls=field.rel.to
		try:
			related_object=kls.objects.all().order_by('?')[0]
			return related_object
		except IndexError:
			print "Couldn't find a related object for ForeignKey: %s" % field.name
			return None
	
	def generate_SlugField(self,**kwargs):
		"""
		Generates a slug for SlugField's
		"""
		result=self.generate_CharField(**kwargs).replace(" ","_")
		result=re.sub(r'_$',"",result)
		return result
	
	def generate_BooleanField(self,**kwargs):
		"""
		Generates a boolean for BooleanField's
		"""
		return bool(random.randint(0,1))
	
	def generate_EmailField(self,**kwargs):
		"""
		Generates a random lipsum email address.
		"""
		front=words(1,common=False)
		back=words(1,common=False)
		#side to side
		email=front+"@"+back+".com"
		return email
	
	def _decide(self,action,*args,**kwargs):
		"""
		Decides whether or not to give a blank field a value
		"""
		if bool(random.randint(0,1)):return action(*args,**kwargs)
	
	def _generate_image(self, resolution):
		"""
		Generates image
		"""
		assert image_support
		size=map(int,resolution.split('x'))
		im=Image.new('RGB',size)
		draw=ImageDraw.Draw(im)
		def _gen_rgb():
			return "rgb%s" % str(tuple([random.randint(0,255) for i in range(3)]))    
		text_pos=(0,0)
		text=[random.choice(string.letters) for i in range(2)]
		draw.rectangle([(0,0),tuple(size)],fill=_gen_rgb())
		for i in range(2):
			fontfile=random.choice(fonts)
			font=ImageFont.truetype("%s/fonts/%s" %(ROOT,fontfile), size[0],encoding=TTF_ENCODING)
			draw.text(text_pos, text[i],fill=_gen_rgb(),font=font)
		filename="%s.png" % self.generate_SlugField(unique=True)
		im.save("%s%s"%(FAKE_UPLOAD_PATH,filename),'PNG')
		return filename
	
	def fill(self,field,obj,dilla=None):
		"""
		Does the work to fill model instances with random data
		"""
		val=None
		field_extras=None
		if dilla:
			field_extras=getattr(dilla,'field_extras',None)
			if field_extras:field_extras=field_extras.get(field.name,None)
			skip_fields=getattr(dilla,'skip_fields',None)
			if skip_fields and field.name in skip_fields:
				print 'Skipping field: %s' % field.name
				return
			if field_extras and field_extras.get("random_values",None):
				vals=field_extras.get('random_values')
				val=vals[random.randrange(0,len(vals))]
			image_fields=getattr(dilla,'image_fields',None)
			generate_images=getattr(dilla,'generate_images',False)
			if image_fields and generate_images and field.name in image_fields:
				resolution=getattr(obj.Dilla,'resolution','640x480')
				resolutions=getattr(obj.Dilla,'resolutions',None)
				if resolutions:
					resolution=obj.Dilla.resolutions[random.randrange(0,len(obj.Dilla.resolutions))]
				if field_extras:
					resolution=field_extras.get("resolution","800x600")
					if field_extras.get("resolutions",None):
						sizes=field_extras.get("resolutions",None)
						if sizes: resolution=sizes[random.randrange(0,len(sizes))]
					else: resolution="800x600"
				if(image_support):val=self._generate_image(resolution)
		if field_extras:
			generator=field_extras.get("generator",None)
			if callable(generator):
				if field_extras.get("generator_wants_extras",None):val=generator(field_extras)
				else: val=generator()
			elif isinstance(generator,str):
				if hasattr(self,generator):
					method=getattr(self,generator)
					val=method()
		if not val:
			internal_type=field.get_internal_type()
			if isinstance(field,URLField):
				val=self.generate_URLField(field=field,unique=field.unique,max_length=field.max_length,field_extras=field_extras)
			elif hasattr(self,"generate_%s"%internal_type):
				generate_method=getattr(self,"generate_%s"%internal_type)
				val=generate_method(field=field,unique=field.unique,max_length=field.max_length,field_extras=field_extras)
		setattr(obj,field.name,val)

More like this

  1. Improved Pickled Object Field by taavi223 4 years, 8 months ago
  2. Command to make fixtures. by buriy 5 years, 8 months ago
  3. db_dump.py - for dumpping and loading data from database by limodou 7 years, 1 month ago
  4. Command to dump data as a python script by willhardy 5 years, 10 months ago
  5. Lorem Ipsum - Random content for your mockups by gfranxman 7 years, 1 month ago

Comments

danny_adair (on March 25, 2010):

Hey that's cool!

Probably better to randomly select a choice if "choices" were specified for a CharField:

GENDER_CHOICES = (
    ('M', 'Male'),
    ('F', 'Female'),
)
gender = models.CharField(
    max_length=1,
    choices=GENDER_CHOICES
)

#

bl4th3rsk1t3 (on March 25, 2010):

Hey Danny, thanks for the update. I'll get that added in.

#

tvon (on March 26, 2010):

Quick note, to support PostgreSQL (psycopg2 specifically), something along these lines:

if settings.DATABASE_ENGINE == 'postgresql_psycopg2':
    import psycopg2
    IntegrityError = psycopg2.IntegrityError
elif settings.DATABASE_ENGINE == 'mysql':
    import MySQLdb
    IntegrityError = MySQLdb.IntegrityError
else:
    # XXX: ADD OTHER DB SUPPORT HERE
    IntegrityError = Exception

Then just catch IntegrityError below instead of MySQLdb.IntegrityError..

#

tvon (on March 26, 2010):

Other comments (as I mess with this):

  • I realize you have gitweb, but it would be nice to have this on github.
  • I'm generating data for testing, but don't really want to add code to my models.py for this. I don't have an alternative in mind, but it would be great if there was a way to load up model generation info from a more segregated file.

#

tvon (on March 26, 2010):

re: IntegrityError, actually they take care of that in django.db, so just "from django.db import IntegrityError"

#

(Forgotten your password?)