Django

Code

Changeset 8775

Show
Ignore:
Timestamp:
08/31/08 16:14:46 (3 months ago)
Author:
brosner
Message:

Enforce max_num=1 on inline model formsets that have a unique foreign key to its parent. I snuck in a quick clean up to the inlineformset_factory as well.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/forms/models.py

    r8756 r8775  
    425425    """ 
    426426    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 
    429430    if exclude is not None: 
    430431        exclude.append(fk.name) 
    431432    else: 
    432433        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) 
    438446    FormSet.fk = fk 
    439447    return FormSet 
  • django/trunk/tests/modeltests/model_formsets/models.py

    r8756 r8775  
    5454    def __unicode__(self): 
    5555        return "%s at %s" % (self.name, self.place) 
     56 
     57class 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) 
    5662 
    5763class OwnerProfile(models.Model): 
     
    530536[<OwnerProfile: Joe Perry is 55>] 
    531537 
     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 
    532547# Foreign keys in parents ######################################## 
    533548