Django UpdatView - custom form fields - django

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',
]

Related

django How get creator username instead id using two for

I want get creator username instead id.
The item.r.username dont work.
item.r.bornplace work correctly.
Where i do mistake?
My model.py:
class Rec(models.Model):
creator = models.ForeignKey(auth.get_user_model(), on_delete=models.CASCADE)
bornplace = models.CharField(default='default')
My views.py
def lists(request):
list = Rec.objects.all()
lists = []
for r in list:
lists.append({'r':r})
context = {'lists': lists}
return render(request, 'lists.html', context)
My lists.html
{% for item in lists %}
{{ item.r.username }}
{{ item.r.author_id }}
{{ item.r.bornplace }}
{% endfor %}
For your issue you should use the Foreign Key mapping in the model fields you have defined from creator as defined below:
class Rec(models.Model):
...
creator = models.ForeignKey(auth.get_user_model(), on_delete=models.CASCADE)
In this instance I can see you are using Django's auth, and without going into too much detail their default model stores username, so from that your username attr can be accessed from your creator instance. With that, overall your template call should have been:
{{ item.r.creator.username }}
But this idea is not specific to Django's auth system, for all FK relationships defined in your models you can use the standard syntax to access fields as needed, both in templates and in views.
Models have attributes so use nested access to gather the values you need. For example:
model.FKField.field or model.FKField.method()
Naturally in templates there are no parentheses, so for using a method in a template the syntax is:
{{ model.FKField.method }}
you should do it in this way:
{{ item.r.creator.username }}

Force Django to recognize a model field value as a template tag value?

I am allowing users to create custom templates, and when doing so they are building off of my own base template with context variables.
So my own template that is used as a base will have a line such as..
<h3>1.0: Contract Specifics for {{ first_name }}</h3>
Now when the user edits this form I am escaping the curly brackets so they store as actual curly brackets in the form field when the user saves their custom template.
Now a user will click save and in the model field there will be raw HTML stored as a TextField and is stored in the database as such...
Now I need to render this in my django template later on but, I need {{ first_name }} to be replaced the actual context variable. unfortunately in my django template when I render this specific form field to display the HTML
{% autoescape off %}
{{ contract_detail }}
{% endautoescape %}
it displays as..
1.0: Contract specifics for {{first_name}}
Is there anyway to force django to recognize this as a template variable and render this correctly, maybe I need some sort of custom functionality, or templatetag?

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.

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.

Django ForeignKey form field: display value

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 }}