Skip to content

How to handle non-admin polymorphic forms? #346

@ShaheedHaque

Description

@ShaheedHaque

Let say I have an Employee model like this:

class Employee(models.Model):
    employee_id = models.CharField(...)
    company = models.ForeignKey(Company, ...)

and I'd like to attach a polymorphic address (to handle country-specific addressing rules). I create a "base" polymorphic class which contains a read-only "jurisdiction" like this:

class PostalDetail(PolymorphicModel):
    jurisdiction = models.CharField(max_length=ISO3166_MAX_LENGTH, editable=False)
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    line1 = models.CharField(max_length=LINE_MAX_LENGTH, help_text=_("First line of address"))
    ...

I can now define subclasses of PostalDetail to cover GB, US, whatever. Depending on which subclass it is, I can programmatically fill in the jurisdiction (based, as it happens, on the subclass model name). Now, using the admin facilities provided by django-polymorphic, I can create an admin interface based on PostalDetail that will let me create records of GB or US-specific flavours.

However, I'm left wondering how to create non-admin forms, like normal Django model forms, to create these addresses. I can see the formset support, but some guidance/exmaples/hints on how to proceed without formsets would be welcome.

(That would allow me to merge the PostalDetails into a polymorphic Employee.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions