tags:

views:

25

answers:

1

Django ContentTypes provides a GenericInlineFormSet, however the documentation does not explain how to use it, except for this test, which doesn't really explain it in a way I understand.

Please can you help me understand it?

Let's say I have the following classes

class Dog(models.Model):
    name = models.CharField(max_length=64)
    breed = models.CharField(blank=True, max_length=64)

    class Meta:
        verbose_name = 'Dog'


class Fish(models.Model):
    name = models.CharField(max_length=64)
    habitat = models.CharField(blank=True, max_length=64)

    class Meta:
        verbose_name = 'Fish'


class Pet(models.Model):
    content_type = models.ForeignKey(
                               ContentType,
                               limit_choices_to={'model__in':('dog', 'fish')},
                               verbose_name='Species'
                               )
    object_id = models.CharField(max_length=64,  verbose_name='Animal')
    object = generic.GenericForeignKey('content_type', 'object_id')

    owner = models.ForeignKey(Owner)

    class Meta:
        unique_together = [("content_type", "object_id")]

What does the view look like to display a form for a Pet?

A: 

GenericInlineFormSet works just like a standard inline formset, except that it uses generic relations rather than standard foreign keys.

Daniel Roseman
Sure, that explains why it's called what it is! So how would you create one for the above example?
Rich