from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def as_json(self):
        return {
            'pk': self.pk,
            'question': self.question,
            'choices': map(lambda e: e.as_json(), self.choice_set.all())
        }

class Choice(models.Model):
    poll = models.ForeignKey(Poll, blank=True, null=True)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def as_json(self):
        return {
            'pk': self.pk,
            'choice_text': self.choice_text,
            'votes': self.votes
        }


# Be careful when modifying codes below!
from django.forms import ModelForm as _ModelForm
_dict = {}
_env = globals()
import inspect as _inspect
for key, value in _env.items():
    if _inspect.isclass(value) and issubclass(value, models.Model):
        class Form_anonymous(_ModelForm):
            class Meta:
                model = value
        _dict[key] = Form_anonymous

for key, value in _dict.iteritems():
    _env[key+'Form'] = value
