Changeset 8775
- Timestamp:
- 08/31/08 16:14:46 (3 months ago)
- Files:
-
- django/trunk/django/forms/models.py (modified) (1 diff)
- django/trunk/tests/modeltests/model_formsets/models.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/forms/models.py
r8756 r8775 425 425 """ 426 426 fk = _get_foreign_key(parent_model, model, fk_name=fk_name) 427 # let the formset handle object deletion by default 428 427 # enforce a max_num=1 when the foreign key to the parent model is unique. 428 if fk.unique: 429 max_num = 1 429 430 if exclude is not None: 430 431 exclude.append(fk.name) 431 432 else: 432 433 exclude = [fk.name] 433 FormSet = modelformset_factory(model, form=form, 434 formfield_callback=formfield_callback, 435 formset=formset, 436 extra=extra, can_delete=can_delete, can_order=can_order, 437 fields=fields, exclude=exclude, max_num=max_num) 434 kwargs = { 435 'form': form, 436 'formfield_callback': formfield_callback, 437 'formset': formset, 438 'extra': extra, 439 'can_delete': can_delete, 440 'can_order': can_order, 441 'fields': fields, 442 'exclude': exclude, 443 'max_num': max_num, 444 } 445 FormSet = modelformset_factory(model, **kwargs) 438 446 FormSet.fk = fk 439 447 return FormSet django/trunk/tests/modeltests/model_formsets/models.py
r8756 r8775 54 54 def __unicode__(self): 55 55 return "%s at %s" % (self.name, self.place) 56 57 class Location(models.Model): 58 place = models.ForeignKey(Place, unique=True) 59 # this is purely for testing the data doesn't matter here :) 60 lat = models.CharField(max_length=100) 61 lon = models.CharField(max_length=100) 56 62 57 63 class OwnerProfile(models.Model): … … 530 536 [<OwnerProfile: Joe Perry is 55>] 531 537 538 # ForeignKey with unique=True should enforce max_num=1 539 540 >>> FormSet = inlineformset_factory(Place, Location, can_delete=False) 541 >>> formset = FormSet(instance=place) 542 >>> for form in formset.forms: 543 ... print form.as_p() 544 <p><label for="id_location_set-0-lat">Lat:</label> <input id="id_location_set-0-lat" type="text" name="location_set-0-lat" maxlength="100" /></p> 545 <p><label for="id_location_set-0-lon">Lon:</label> <input id="id_location_set-0-lon" type="text" name="location_set-0-lon" maxlength="100" /><input type="hidden" name="location_set-0-id" id="id_location_set-0-id" /></p> 546 532 547 # Foreign keys in parents ######################################## 533 548
