How can I pass the value of a variable to the value attribute of a field in my form? I have tried overriding the init method but I can't get it to work. This is my code.
models.py:
class Profile(models.Model):
user = models.OneToOneField(UserManegement, related_name='user', on_delete=models.CASCADE)
avatar = models.ImageField(upload_to=custom_upload_to, null=True, blank=True)
first_name = models.TextField(user, null=True, blank=True)
last_name = models.TextField(user, null=True, blank=True)
phone = models.TextField(user, null=True, blank=True)
forms.py:
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['avatar', 'first_name','last_name', 'phone']
widgets = {
'avatar': forms.ClearableFileInput(attrs={'class': 'form-control-file mt-3'}),
'first_name': forms.TextInput(attrs={'class': 'form-control mt-3', 'placeholder': 'Nombre'}),
'last_name': forms.TextInput(attrs={'class': 'form-control mt-3', 'placeholder': 'Apellidos'}),
'phone': forms.TextInput(attrs={'class': 'form-control mt-3', 'placeholder':'Teléfono'}),
}
views.py:
@method_decorator(login_required, name='dispatch')
class ProfileUpdate(UpdateView):
form_class = ProfileForm
success_url = reverse_lazy('profile')
template_name = 'registration/profile_form.html'
def get_object(self):
#recuperamos el objeto para editarlo
profile, created = Profile.objects.get_or_create(user=self.request.user)
return profile
In this case, I would like to add the first time the form is loaded, the first_name field with the value of request.user.first_name.
Thank you very much!