Django ForeignKey form field: display value - django

I might just be having a brain fart, but I seriously can't figure this out. How do I get display the values of ForeignKey fields from a bound form in a template? {{ field }} renders a widget, {{ field.value }} returns the pk, and I need the model itself.
I am writing a custom template for django-crispy-forms; which is why I only get a form field in my context, but not a model field.

You must try this: {{ form.instance.field }}

Related

Django Updateview not saving when in html but saves in admin

I have a big form, some fields, depends on other fields value to compute.
Since so, i used jquery blur to submit the form each time user done editing a field and redirect it to updateview again with new values.
I have this day_absent and amount absent to test this.
If i just put form.day_absent in the form and put object.amt_absent, it doesnt save the form. But if i put form.amt_absent, it saves the form.
Why is that? I do not wish to put amt_absent as editable, it should just be the day_absent.
class PayrollTransactionUpdate(LoginRequiredMixin,UpdateView):
model = t_pay
template_name = 'payroll/transaction/update.html'
fields = [
'day_absent',
'amt_absent',
]
<div id='input-item' class="col-1">
{{ form.day_absent }}
{{ form.day_absent.errors}}
</div>
<div id='item' class="col-2">
{{ object.amt_absent }}
</div>
Couple of points:
When you use {{object.amt_absent}}, you are not rendering the form for the field amt_absent, rather just the value saved in the DB.
You added amt_absent as a field to be included in the UpdateView, so, until and unless this field is not required, the form will keep throwing error for this field. That is why your form works when you include both the fields. You can check all errors by using {{form.errors}} or field specified errors by using {{form.amt_absent.errors}} in your template.

Django UpdatView - custom form fields

I have my UpdateView for updating some of my data.
class WpisUpdate(UpdateView):
model=ProdukcjaStanTb
fields=[
'temat',
'podtemat',
'proc_wym',
'proc_auto',
'proc_palnik',
'proc_uruch',
'proc_pakowanie',
]
Now, in my template I'd like to have only :
'proc_wym',
'proc_auto
'proc_palnik',
'proc_uruch',
'proc_pakowanie',
Fields, but also have access to fields "temat" and "podtemat" (to make big titles or web page title for example). In template i'm using {{form.temat.value}} tags, which are ok, but requires those fields in field list in UpdateView. I don't want user to change that. Is there any quick way to have those fields hidden in form but accessible while using easy:
{{ form.as_p }}
in template ? Or do i have to manually edit my form and add some html attributes, like read-only or input type="hidden" ?
Since this view only updates object you can always exclude unnecessary fields from autogenerated modelform - simply del them from your fields declaration then access 'temat', 'podtemat' in your template using object
template
{{ object.temat }} {{ object.podtemat }} {{ form.as_p }}
view
class WpisUpdate(UpdateView):
model = ProdukcjaStanTb
fields=[
'proc_wym',
'proc_auto',
'proc_palnik',
'proc_uruch',
'proc_pakowanie',
]

Get object fields from ForeignKey in RadioSelect

I have a model with a ForeignKey field and a form to create and update the objects.
I choose the ForeignKeyField with a RadioSelect widget and loops through the radio inputs in the template with {% for radio in form.foreign_key_items %}{{ radio.tag }} - {{ radio.choice_label }}{% endfor %}.
My form is:
class ItemForm(ModelForm):
class Meta:
model = Item
fields = ('foreign_key_item',)
widgets = {'foreign_key_item': RadioSelect,}
But the choice_label is not enough information for the user to select the correct object.
How is it possible to print fields from the foreign_key_item objects when I print each radio?
You can compile all the appropriate information in the view and add it to the context that is rendering the form. Alternatively, you can probably use select_related.

How to check django ModelForm field type in html?

I need to do some logic in HTML page depending on field type.
For example something to do if input is type="text" or if it is textarea or checkbox....
{% for field in formFields %}
{{field}}
{% endfor %}
When i do something like this: {{field.field}} i get something like this:
<django.forms.fields.IntegerField object at 0x04812750>
How to use this or is better way?
Specify field type in your views(or forms.py), do not contain complex logic in your template for field rendering,
class FormClass(ModelForm):
my_field = forms.CharField(widget=forms.Textarea)
Then my_field will be rendered as textarea in template, each field type has its own widget, read the docs here.

django custom form template checkbox question

Django forms are really easy and nice to style especially if you like to take control of the outcome.
A question. Is there anyway you can see whether the {{ field }} type, ie checkbox, radio etc?
you could create a field_type template filter
{{ field|field_type }} = CharField
{{ field|widget_type }} = TextInput
heres a great example :
http://olivergeorge.posterous.com/django-template-tags-to-find-out-field-type