My question is quite close to this one UpdateView with additionnals fields,
so I used the info, but I still lack some details to complete the process
So, I have a simple toy model :
class TxtPlus(models.Model):
txt = models.CharField(max_length=140)
def __str__(self):
return f'TxtPlus<{self.id},{self.txt}>'
def get_absolute_url(self):
return reverse("tplus_detail", kwargs={"pk": self.pk})
When editing an instance, I want to add a field, and thus according to the answer above, I define a custom Model
class TxtPlusForm(ModelForm):
info = CharField(widget=Textarea(attrs={"rows":"2"}))
class Meta:
model = TxtPlus
fields = ["txt", ]
the using the UpdateView is easy
class TxtPlusUpdateView(UpdateView):
model = TxtPlus
template_name = "adding_to_model_forms/tplus_update.html"
form_class = TxtPlusForm
But, what I wish to do is roughly:
def infos_associated_to_object(object):
return f'there is more about object:{object.pk}'
def test_edit(request,pk):
object = TxtPlus.objects.get(id=pk)
info = infos_associated_to_object(object)
if request.method == 'GET':
form = TxtPlusForm(instance=object,initial={'info': info})
up_log = None
if request.method == 'POST':
form = TxtPlusForm(request.POST,instance=object)
up_log = f"change '{info}' to '{form.data['info']}'"
form.save()
#... add here form.data['info'] saving
return render(
request,
"adding_to_model_forms/tplus_edit.html",
{
"object" : object,
"form" : form,
"info" : info,
"up_log" : up_log
}
)
(of course, infos_associated_to_object, is here a toy version..)
In particular, my problem with UpdateView, is on :
first the part
initial={‘info’: info} (where info = infos_associated_to_object(object) )
and then the part
up_log = f”change ‘{info}’ to ‘{form.data[‘info’]}'”
#… add here form.data[‘info’] saving
Can you give me some pointers on how to do that using the generic UpdateView ?