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