Testing Django Inline ModelForms: How to arrange POST data?

Posted by unclaimedbaggage on Stack Overflow See other posts from Stack Overflow or by unclaimedbaggage
Published on 2010-05-22T08:41:09Z Indexed on 2010/05/22 8:50 UTC
Read the original article Hit count: 658

Filed under:
|
|
|

Hi folks,

I have a Django 'add business' view which adds a new business with an inline 'business_contact' form.

The form works fine, but I'm wondering how to write up the unit test - specifically, the 'postdata' to send to self.client.post(settings.BUSINESS_ADD_URL, postdata)

I've inspected the fields in my browser and tried adding post data with corresponding names, but I still get a 'ManagementForm data is missing or has been tampered with' error when run.

Anyone know of any resources for figuring out how to post inline data?

Relevant models, views & forms below if it helps. Lotsa thanks.

MODEL:

class Contact(models.Model):
    """ Contact details for the representatives of each business """
    first_name = models.CharField(max_length=200)
    surname = models.CharField(max_length=200)
    business = models.ForeignKey('Business')
    slug = models.SlugField(max_length=150, unique=True, help_text=settings.SLUG_HELPER_TEXT)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    phone = models.CharField(max_length=100, null=True, blank=True)
    mobile_phone = models.CharField(max_length=100, null=True, blank=True)
    email = models.EmailField(null=True)
    deleted = models.BooleanField(default=False)
    class Meta:
        db_table='business_contact'

    def __unicode__(self):
        return '%s %s' % (self.first_name, self.surname)

    @models.permalink
    def get_absolute_url(self):
        return('business_contact', (), {'contact_slug': self.slug })

class Business(models.Model):
    """ The business clients who you are selling products/services to """
    business = models.CharField(max_length=255, unique=True)
    slug = models.SlugField(max_length=100, unique=True, help_text=settings.SLUG_HELPER_TEXT)
    description = models.TextField(null=True, blank=True)
    primary_contact = models.ForeignKey('Contact', null=True, blank=True, related_name='primary_contact')
    business_type = models.ForeignKey('BusinessType')
    deleted = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    address_1 = models.CharField(max_length=255, null=True, blank=True)
    address_2 = models.CharField(max_length=255, null=True, blank=True)
    suburb = models.CharField(max_length=255, null=True, blank=True)
    city = models.CharField(max_length=255, null=True, blank=True)
    state = models.CharField(max_length=255, null=True, blank=True)
    country = models.CharField(max_length=255, null=True, blank=True)
    phone = models.CharField(max_length=40, null=True, blank=True)
    website = models.URLField(null=True, blank=True)
    class Meta:
        db_table = 'business'

    def __unicode__(self):
        return self.business

    def get_absolute_url(self):
        return '%s%s/' % (settings.BUSINESS_URL, self.slug)

VIEWS:

class Contact(models.Model):
    """ Contact details for the representatives of each business """
    first_name = models.CharField(max_length=200)
    surname = models.CharField(max_length=200)
    business = models.ForeignKey('Business')
    slug = models.SlugField(max_length=150, unique=True, help_text=settings.SLUG_HELPER_TEXT)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    phone = models.CharField(max_length=100, null=True, blank=True)
    mobile_phone = models.CharField(max_length=100, null=True, blank=True)
    email = models.EmailField(null=True)
    deleted = models.BooleanField(default=False)
    class Meta:
        db_table='business_contact'

    def __unicode__(self):
        return '%s %s' % (self.first_name, self.surname)

    @models.permalink
    def get_absolute_url(self):
        return('business_contact', (), {'contact_slug': self.slug })

class Business(models.Model):
    """ The business clients who you are selling products/services to """
    business = models.CharField(max_length=255, unique=True)
    slug = models.SlugField(max_length=100, unique=True, help_text=settings.SLUG_HELPER_TEXT)
    description = models.TextField(null=True, blank=True)
    primary_contact = models.ForeignKey('Contact', null=True, blank=True, related_name='primary_contact')
    business_type = models.ForeignKey('BusinessType')
    deleted = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    address_1 = models.CharField(max_length=255, null=True, blank=True)
    address_2 = models.CharField(max_length=255, null=True, blank=True)
    suburb = models.CharField(max_length=255, null=True, blank=True)
    city = models.CharField(max_length=255, null=True, blank=True)
    state = models.CharField(max_length=255, null=True, blank=True)
    country = models.CharField(max_length=255, null=True, blank=True)
    phone = models.CharField(max_length=40, null=True, blank=True)
    website = models.URLField(null=True, blank=True)
    class Meta:
        db_table = 'business'

    def __unicode__(self):
        return self.business

    def get_absolute_url(self):
        return '%s%s/' % (settings.BUSINESS_URL, self.slug)

FORMS:

class AddBusinessForm(ModelForm):
    class Meta:
        model = Business
        exclude = ['deleted','primary_contact',]

class ContactForm(ModelForm):
    class Meta:
        model = Contact
        exclude = ['deleted',]


AddBusinessFormSet = inlineformset_factory(Business,
        Contact,
        can_delete=False,
        extra=1,
        form=AddBusinessForm,
        )

© Stack Overflow or respective owner

Related posts about django

Related posts about testing