1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Changed the example a bit, did it help at all? Subclassing a ModelForm isnt far from copy-pasting the original modelform imho? At least not with minor changes as different excludes/fields, would be nice to be able to change this at initialization, like: f = MyModelForm(exclude=('field1', 'field2'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.