|
@@ -15,15 +15,17 @@ from rapid.wrappers import FieldData, ModelData, InstanceData
|
|
|
class RapidAlternativesField(forms.Field):
|
|
|
def __init__(self, field_name, alternatives, selector_name, form, request, instance=None, *args, **kwargs):
|
|
|
model = None
|
|
|
+ if instance:
|
|
|
+ model = getattr(instance, selector_name)
|
|
|
if form.base_fields[selector_name].initial:
|
|
|
- model = ContentType.objects.get(pk=form.base_fields[selector_name].initial)
|
|
|
+ model = ContentType.objects.get_for_id(form.base_fields[selector_name].initial)
|
|
|
alt = []
|
|
|
for a in alternatives:
|
|
|
md = ModelData(a)
|
|
|
ft = for_model(request, a)
|
|
|
prefix = field_name + '_' + str(md.content_type().pk)
|
|
|
- selected = model == a
|
|
|
- inst = instance if selected else None
|
|
|
+ selected = ContentType.objects.get_for_model(a) == model
|
|
|
+ inst = getattr(instance, field_name) if selected else None
|
|
|
if request.method == 'POST':
|
|
|
fm = ft(request.POST, request.FILES, prefix=prefix, instance=inst)
|
|
|
else:
|
|
@@ -70,10 +72,11 @@ def for_model(request, model, default_relations=()):
|
|
|
initial[k] = v
|
|
|
if initial:
|
|
|
kwargs['initial'] = initial
|
|
|
+ instance = kwargs.get('instance')
|
|
|
for n, f in ModelData(model).rapid_alternative_data():
|
|
|
ct = ModelData(model).field_by_name(f.ct_field).field
|
|
|
fk = ModelData(model).field_by_name(f.fk_field).field
|
|
|
- fl = RapidAlternativesField(n, ct.alternatives, ct.name, self, request, initial)
|
|
|
+ fl = RapidAlternativesField(n, ct.alternatives, ct.name, self, request, instance)
|
|
|
type(self.__class__).__setattr__(self.__class__, n, fl)
|
|
|
nd = collections.OrderedDict()
|
|
|
for k, v in self.__class__.base_fields.iteritems():
|