| 314 | | def inline_formset(parent_model, model, fk_name=None, fields=None, extra=3, orderable=False, deletable=True, formfield_callback=lambda f: f.formfield()): |
|---|
| 315 | | """ |
|---|
| 316 | | Returns an ``InlineFormset`` for the given kwargs. |
|---|
| 317 | | |
|---|
| 318 | | You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey`` |
|---|
| 319 | | to ``parent_model``. |
|---|
| 320 | | """ |
|---|
| | 314 | def get_foreign_key(parent_model, model, fk_name=None): |
|---|
| | 315 | """ |
|---|
| | 316 | Finds and returns the ForeignKey from model to parent if there is one. |
|---|
| | 317 | If fk_name is provided, assume it is the name of the ForeignKey field. |
|---|
| | 318 | """ |
|---|
| | 319 | # avoid circular import |
|---|
| 323 | | # figure out what the ForeignKey from model to parent_model is |
|---|
| 324 | | if fk_name is None: |
|---|
| | 322 | if fk_name: |
|---|
| | 323 | fks_to_parent = [f for f in opts.fields if f.name == fk_name] |
|---|
| | 324 | if len(fks_to_parent) == 1: |
|---|
| | 325 | fk = fks_to_parent[0] |
|---|
| | 326 | if not isinstance(fk, ForeignKey) or fk.rel.to != parent_model: |
|---|
| | 327 | raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, parent_model)) |
|---|
| | 328 | elif len(fks_to_parent) == 0: |
|---|
| | 329 | raise Exception("%s has no field named '%s'" % (model, fk_name)) |
|---|
| | 330 | else: |
|---|
| | 331 | # Try to discover what the ForeignKey from model to parent_model is |
|---|
| 332 | | else: |
|---|
| 333 | | fks_to_parent = [f for f in opts.fields if f.name == fk_name] |
|---|
| 334 | | if len(fks_to_parent) == 1: |
|---|
| 335 | | fk = fks_to_parent[0] |
|---|
| 336 | | if not isinstance(fk, ForeignKey) or fk.rel.to != parent_model: |
|---|
| 337 | | raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, parent_model)) |
|---|
| 338 | | elif len(fks_to_parent) == 0: |
|---|
| 339 | | raise Exception("%s has no field named '%s'" % (model, fk_name)) |
|---|
| | 339 | return fk |
|---|
| | 340 | |
|---|
| | 341 | def inline_formset(parent_model, model, fk_name=None, fields=None, extra=3, orderable=False, deletable=True, formfield_callback=lambda f: f.formfield()): |
|---|
| | 342 | """ |
|---|
| | 343 | Returns an ``InlineFormset`` for the given kwargs. |
|---|
| | 344 | |
|---|
| | 345 | You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey`` |
|---|
| | 346 | to ``parent_model``. |
|---|
| | 347 | """ |
|---|
| | 348 | fk = get_foreign_key(parent_model, model, fk_name=fk_name) |
|---|