Readonly ModelChoiceField of type in model foreignkey

492 views
Skip to first unread message

nelson fernando garcia gomez

unread,
Sep 18, 2018, 2:58:26 PM9/18/18
to Django users
Hola estoy tratando en mi formulario (formsModel) implementar el atributo de readonly  a una foreignkey (ModelChoiceField)
pero no es posible alguna ayuda por favor GRACIAS así lo estoy haciendo: 
class CostoForm(forms.ModelForm):

"""
Edicion de Costos con formset.
"""

class Meta:
    model
= Costo
    exclude
= ('user', )

def __init__(self, *args, **kwargs):
   
super(CostoForm, self).__init__(*args, **kwargs)
   
self.helper = FormHelper()
   
self.helper.form_tag = False
   
self.helper.form_action = ''
   
self.helper.form_class = 'form-inline'
   
self.helper.template = 'crispy_template/table_inline_formset.html'
   
self.helper.layout = Layout(
       
Field('DELETE', css_class='btn btn-primary')
   
)
   
self.helper.add_input(Submit('submit', _('Aceptar')))
   
self.fields['destination'].widget.attrs['readonly'] = True



Mateusz

unread,
Sep 18, 2018, 8:00:37 PM9/18/18
to Django users
(use traductor si necesita)

I hope I understand your question correctly.

When in Django 1.8 or 1.9+ you can just do
self.fields['destination'].disabled = True

in lower versions you should add
self.fields['destination'].widget.attrs['disabled'] = True
at the end of your __init__(..).
Message has been deleted

nelson fernando garcia gomez

unread,
Sep 19, 2018, 9:47:16 AM9/19/18
to Django users
This only disables it but internally it receives data this makes the process of loading the template is the same with disabled enabled or disabled, so it is necessary to use readonly but does not show readonly. 

Mateusz

unread,
Sep 19, 2018, 7:07:35 PM9/19/18
to Django users
Sorry but I don't understand. For me, what you say now is that you want the field to be visible but empty (without any data from model in it) or invisible (excluded)? Can you, please, clarify what do you want to achieve?
Have you tried appending the lines I gave you, not replacing the last line? (both disabled and readonly)

nelson fernando garcia gomez

unread,
Sep 19, 2018, 11:34:57 PM9/19/18
to Django users
to have a select field with 2000 records I want it to be only reading to show me the field to which is linked only reading 
if I put it in disabled mode I deactivate the ok field to this point well, the bad thing is that putting it this way, the 2000 records are still loaded into the template and this makes the face time is great.



Declinación de Responsabilidades: Los servicios de MISENA son soportados tecnológicamente por © Google y ofrecidos por el Servicio Nacional de Aprendizaje – SENA de manera gratuita a los aprendices e instructores de programas de formación titulada, las opiniones que contenga este mensaje son exclusivas de su autor y no representan la opinión del Servicio Nacional de Aprendizaje o de sus autoridades. El receptor deberá verificar posibles virus informáticos que tenga el correo o cualquier anexo, razón por la cual el SENA no es responsable de los daños causados por cualquier virus transmitido en este correo electrónico.

Los contenidos, textos, imágenes, archivos enviados en este mensaje son responsabilidad exclusiva del remitente y no reflejan ni comprometen de ninguna manera a la institución. No se autoriza el uso de esta herramienta para el intercambio de correos masivos, cadenas o spam, ni de mensajes ofensivos, de carácter político, sexual o religioso, con fines de lucro, con propósitos delictivos o cualquier otro mensaje que se considere indebido o que vaya en contra de la Ley.

nelson fernando garcia gomez

unread,
Sep 19, 2018, 11:35:40 PM9/19/18
to Django users
a possible solution I see is to change the field to CharField() type, here is a new problem: the field is printed the pk that is related as then how can I read that pk to show the name and not that pk in the template?

nelson fernando garcia gomez

unread,
Sep 19, 2018, 11:38:58 PM9/19/18
to Django users

to have a select field with 2000 records I want it to be only reading to show me the field to which is linked only reading 
if I put it in disabled mode I deactivate the ok field to this point well, the bad thing is that putting it this way, the 2000 records are still loaded into the template and this makes the face time is great.

a possible solution I see is to change the field to CharField() type, here is a new problem: the field is printed the pk that is related as then how can I read that pk to show the name and not that pk in the template?


El miércoles, 19 de septiembre de 2018, 18:07:35 (UTC-5), Mateusz escribió:
Reply all
Reply to author
Forward
0 new messages