Login

Debug data for forms

Author:
girasquid
Posted:
April 21, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

I sometimes find that larger forms need data associated with them, but it's a bit of a pain to retype it each time when I'm debugging. The DebugForm base class lets you specify sets of testing data that will be inserted into your form if your project is in debug mode, randomly chosen from the DEBUG_DATA dict.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django.conf import settings
import random

class DebugForm(forms.ModelForm):
    
    def __init__(self, *args, **kwargs):
        super(DebugForm, self).__init__(*args, **kwargs)
        if kwargs.get('instance'):
            return
        if settings.DEBUG:
            for field in self.DEBUG_DATA.keys():
                val = random.choice(self.DEBUG_DATA[field])
                self.fields[field].initial = val

class MyForm(DebugForm):
    
    DEBUG_DATA = {
        'comment':['this is some sample data', 'this is some more sample data']
    }
    
    class Meta:
        
        model = YourModel

More like this

  1. LazyPrimaryKeyRelatedField by LLyaudet 2 days, 18 hours ago
  2. CacheInDictManager by LLyaudet 3 days, 1 hour ago
  3. MYSQL Full Text Expression by Bidaya0 3 days, 19 hours ago
  4. Custom model manager chaining (Python 3 re-write) by Spotted1270 1 week, 3 days ago
  5. Django Standard API Response Middleware for DRF for modern frontend easy usage by Denactive 3 weeks, 4 days ago

Comments

jsandell (on May 13, 2009):

Haven't tried it out, but that looks pretty cool. I've always just filled the form out once, recorded it with Molybdenum, and then "replayed" the form each time there after.

I'll definitely have to give this a whirl. :D

#

girasquid (on November 13, 2009):

And I'll have to take a look at Molybdenum :)

#

Please login first before commenting.