def field_changed(request):
    """
    Ajax callback called when a trigger field or base field has changed. Returns
    html for new options and details for the dependent field as json.
    """
    hashed_name = request.POST.get('hashed_name')
    app_label, model_name, base_field_name = hashed_name.split('__')
    model = apps.get_model(app_label, model_name)
    obj = FlexSelectWidget.object_from_post(model, request.POST)
    value_fk = getattr(obj, base_field_name)
    admin_instance = admin.site._registry[obj.__class__]
    base_field = next(f for f in obj._meta.fields if f.name == base_field_name)
    widget = admin_instance.formfield_for_dbfield(
        base_field,
        request=request,
    ).widget.widget

    if bool(int(request.POST['include_options'])):
        if widget.choice_function:
            choices = widget.choice_function(obj)
        else:
            choices = choices_from_instance(obj, widget)

        args = [[value_fk.pk if value_fk else None]]
        if DJANGO_VERSION < (1, 10, 0):
            args.insert(0, [])
        options = Select(choices=choices).render_options(*args)
    else:
        options = None

    return HttpResponse(json.dumps({
        'options': options,
        'details': details_from_instance(obj, widget),
    }), content_type='application/json')
Exemple #2
0
def field_changed(request):
    """
    Ajax callback called when a trigger field or base field has changed. Returns
    html for new options and details for the dependent field as json.
    """
    hashed_name = request.POST.__getitem__('hashed_name')
    widget = FlexSelectWidget.instances[hashed_name]
    instance = instance_from_request(request, widget)
    try:
        value_fk = getattr(instance, widget.base_field.name)
    except:
        value_fk = None
    if bool(int(request.POST.__getitem__('include_options'))):
        choices = choices_from_instance(instance, widget)
        options = Select(choices=choices).render_options(
            [], [value_fk.pk if value_fk else None])
    else:
        options = None

    return HttpResponse(json.dumps({
        'options':
        options,
        'details':
        details_from_instance(instance, widget),
    }),
                        content_type='application/json')
Exemple #3
0
def field_changed(request):
    """
    Ajax callback called when a trigger field or base field has changed.
    Returns html for new options and details for the dependent field as json.
    """
    hashed_name = request.POST.get('hashed_name')
    app_label, model_name, base_field_name = hashed_name.split('__')
    model = apps.get_model(app_label, model_name)
    obj = FlexSelectWidget.object_from_post(model, request.POST)
    try:
        value_fk = getattr(obj, base_field_name)
    except ObjectDoesNotExist:
        value_fk = None
    admin_instance = admin.site._registry[obj.__class__]
    base_field = next(f for f in obj._meta.fields if f.name == base_field_name)
    widget = admin_instance.formfield_for_dbfield(
        base_field,
        request=request,
    ).widget.widget

    if bool(int(request.POST['include_options'])):
        if widget.choice_function:
            choices = widget.choice_function(obj)
        else:
            choices = choices_from_instance(obj, widget)

        args = [[value_fk.pk if value_fk else None]]
        if DJANGO_VERSION < (1, 10, 0):
            args.insert(0, [])
        if DJANGO_VERSION < (1, 11, 0):
            options = Select(choices=choices).render_options(*args)
        else:
            # django 1.11 has introduced template rednering for widgets
            widget.choices = choices
            val = value_fk.pk if value_fk else None
            options = widget.render_options_template(val, widget.attrs)
    else:
        options = None

    return HttpResponse(json.dumps({
        'options':
        options,
        'details':
        details_from_instance(obj, widget),
    }),
                        content_type='application/json')
Exemple #4
0
def field_changed(request):
    """
    Ajax callback called when a trigger field or base field has changed. Returns
    html for new options and details for the dependent field as json.
    """
    hashed_name = request.POST.__getitem__('hashed_name')
    widget = FlexSelectWidget.instances[hashed_name]
    instance = instance_from_request(request, widget)

    if bool(int(request.POST.__getitem__('include_options'))):
        choices = choices_from_instance(instance, widget)
        options = Select(choices=choices).render_options([], [])
    else:
        options = None

    return HttpResponse(json.dumps({
        'options': options,
        'details': details_from_instance(instance, widget),
    }), mimetype='application/json')
Exemple #5
0
def field_changed(request):
    """
    Ajax callback called when a trigger field or base field has changed. Returns
    html for new options and details for the dependent field as json.
    """
    hashed_name = request.POST.__getitem__('hashed_name')
    widget = FlexSelectWidget.instances[hashed_name]
    instance = instance_from_request(request, widget)

    if bool(int(request.POST.__getitem__('include_options'))):
        choices = choices_from_instance(instance, widget)
        options = CustomSelectWidget(choices=choices).render_options([], [])
    else:
        options = None

    return HttpResponse(
        json.dumps({
            'options': options,
            'details': details_from_instance(instance, widget),
        }))
def field_changed(request):
    """
    Ajax callback called when a trigger field or base field has changed. Returns
    html for new options and details for the dependent field as json.
    """
    hashed_name = request.POST.__getitem__('hashed_name')
    # if hashed_name in FlexSelectWidget.__class__.instances:
    widget = Storage().__class__.instances[hashed_name]
    instance = instance_from_request(request, widget)
    try:
        value_fk = getattr(instance, widget.base_field.name)
    except Exception:
        value_fk = None
    if bool(int(request.POST.__getitem__('include_options'))):
        choices = choices_from_instance(instance, widget)
        options = Select(choices=choices).render_options([], [value_fk.pk if value_fk else None])
    else:
        options = None
    
    return HttpResponse(json.dumps({
        'options' : options,
        'details': details_from_instance(instance, widget),
        }), mimetype='application/json')