Codenerix - Disable a dropdown field to a foreign key using ng-readonly

1

Codenerix

Does anyone know how to correctly use ng-readonly in a GenModelForm when calling from a sublist tab (GenList) which in turn invokes a CreateModal window?

The structure is master-detail. The sublist has the pk of the master table and calls the GenCreateModal with the pk of said master table.

GenCreateModal receives the pk argument in its associated form (the GenModelForm mentioned above) and uses it to fill in the field. The objective is to disable the field with ng-disabled if the pk of the master table is passed and assigned this value. In this way, when we call the creation from a sublist we can block the field and force to assign the data of the master table and if it is called from its own maintenance without indicating the pk of the master table we can select it by not being blocked.

I tried to do it in the following way:

First assign 'client' in GenCreateModal with:

def get_initial(self): 
    client = self.kwargs.get('pk', None) 
    if client: 
        self.kwargs['client'] = client 
    return self.kwargs 

Then read it in the GenModelform with:

def __init__(self, *args, **kwargs):   
    super(DetailForm, self).__init__(*args, **kwargs)
    if kwargs.get('initial', None) and kwargs['initial'].get('client', None):
        self.fields['client'].widget.attrs[u'ng-readonly'] = 'true'

But it does not work correctly, the pull-down is enabled and the value can be modified. It can be done because in templatetags_list.py of codenerix we have:

def inireadonly(attrs, i):
    field = ngmodel(i)
    return addattr(attrs, 'ng-readonly=readonly_{0}'.format(field))

This code sets ng-readonly to "true readonly_client" instead of "true" when it comes with the "true" value already assigned from GenModelForm, the values are being concatenated.

I have found a workaround to force it with:

self.fields['client'].widget.attrs[u'ng-readonly'] = 'true || '

in this way the concatenated value will be "true || readonly_client" which results in "true" when evaluating it, but I do not think it is the correct way.

In a local fork of django-codenerix I changed the function inireadonly to (care that is duplicated, it would have to be changed in both places):

def inireadonly(attrs, i):
    field = ngmodel(i)
    if attrs.get('ng-readonly', None) is None:
        attrs = addattr(attrs, 'ng-readonly=readonly_{0}'.format(field))
    return attrs

In this way the value is respected when it is completed from GenModelForm, but I am not sure that it is correct and does not produce side effects, for example when you want to concatenate values to the expression, this would force you to read the old one, concatenate it what we want and establish it. I think there must be a better way to do it and also the expression 'ng-readonly = readonly_ {0}'. Format (field) has some functionality that I do not know, I do not want to break it or lose it the day I find out, so I undid the changes in search of a better solution.

I am currently using:

self.fields['client'].widget.attrs[u'ng-disabled'] = 'true'

and it works perfectly, I no longer have problems disabling the field, but I have been curious about the correct way to use it with ng-readonly if I need it in the future because with ng-readonly it is allowed to select text in the fields with the mouse to be able to copy it in other places and with ng-disabled is not allowed, this functionality in some cases can be interesting.

Does anyone know how to use ng-readonly with django-codenerix in a correct way? Does anyone know the functionality of 'ng-readonly = readonly_ {0}'. Format (field)?

    
asked by c0nd3m0r 23.11.2017 в 00:58
source

2 answers

0

You should use ng-disabled as you use it right now. This is the way we use it in Examples of Django Codenerix @ Gibhub (lines 41 and 42) and this is the way it is also developed in StaticSelects (line 228).

    
answered by 21.12.2017 в 08:33
0

Having a GenModelForm , when you define __ groups __ , one of the parameters that you can pass to the fields is {'extra': ['ng- disabled = true ']} . Example:

def __groups__(self):
    g = [
        (_('Info'), 12,
            ['client', 6, {'extra': ['ng-disabled=true']}],
        )
    ]
    return g
    
answered by 21.12.2017 в 08:35