schmilblick
schmilblick

Reputation: 1925

Creating complex django forms

I've tried to solve this for a couple of days now and could use a little outside input.

What would be the best way to create Forms for these related models:

STATUSES = (('1', 'Draft'), ('2', 'Active'), ('3', 'Deleted'), ('4', 'Credited'))
class Contract(models.Model):
    details = ForeignKey(Order)
    status = CharField(max_length=1, choices=STATUSES)

class Product1Order(Order):
     items = ManyToManyField(Item)
     # + more product specifics

class Item(models.Model):
    tag = ForeignKey(Tag)
    status = CharField(max_length=1, choices=STATUSES)
    price = PositiveIntegerField()

I started looking in to Formsets, but I couldn't really understand the point of using them for this.
If I use ModelForms the status field will collide on ModelA and the ModelC's, and if I want different fields to show up on different pages in my apps, I would have to copy-paste the modelforms to a new modelform and change the Meta-exclude/fields per Form Object?

If anyone have any hints, I'd be very grateful.

Upvotes: 1

Views: 398

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599956

Firstly, this is too abstract to understand what you want to do. Why are there two status fields? What do the different relationships actually mean? What are the constraints

Secondly, you are confused about formsets. For a start, there's no way of doing multiply nested inline forms - they only work for relationships between parent and children - you can have multiple parent-child relationships, but not parent -> child -> grandchild as you do here. But if it did work, there wouldn't be any collision between the different status fields - they are on different models.

Thirdly, there's no reason to copy-and-paste anything. Formsets are classes, so you can easily subclass them and use different exclude values, or even use a single class with an __init__ method that takes an extra parameter to decide what fields to exclude.

Upvotes: 1

Related Questions