So i've been working on a project for a while, in which I use the Django default User Instance but with additional attributes, which are stored in a "Profile"-Model. So right now i have an assigned company, and an Profile-Image for the user in the Profile Model.
Now i have a detailview to edit the user's attributes (firstname, username, email, etc.). i used generic.DetailView for this View. But this is only working for the Attributes in the User-Model from Django. How can I also change Attributes in the Profile Model when editing the User?
This is my Edit-View:
class ProfileUpdateView(LoginRequiredMixin, UpdateView):
model = User
fields = ['username', 'email', 'first_name', 'last_name']
template_name = 'inventory/edit_forms/update_profile.html'
success_url = '/profile'
login_url = '/accounts/login'
redirect_field_name = 'redirect_to'
def form_valid(self, form):
profile = Profile.objects.get(user=self.request.user)
profile.img = self.request.POST.get('profile_img')
profile.save()
return super().form_valid(form)
def get_object(self):
return User.objects.get(pk = self.request.user.id)
This is my HTML:
<form class="edit_object" action="" method="post" enctype='multipart/form-data' class="form-group">
<div style="width: 20%; margin-bottom: 1rem;">
<label>Profile Image</label>
<input name="profile_img" type="file" accept="image/png, image/gif, image/jpeg">
</div>
{% csrf_token %}
{{form|crispy}}
<input type="submit" value="Submit" class="btn action_btn">
</form>
As you see i already tried using a external Input field and setting the image like that. But after submitting, the img attribute in the Profile-Model is just set null and I don't know why.
What did I miss?