I've be struggling the last to days. I want the current user to be able to add a file without selecting himself the user of the file being uploaded. For now, I need to manually select a user from the list to add a file related to him.
Big thanks in advance!
Hugo
Here's models.py
from django.db import models
from django.contrib.auth.models import User
class Client(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
name = models.CharField(max_length=100, null=True)
def __str__(self):
return self.name
class fichier4(models.Model):
user = models.ForeignKey(Client, on_delete=models.CASCADE)
file = models.FileField()
date_created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.file
my view.py code
form3 = FichierUpload()
initial_data = {
'user' : request.user
}
if request.method == 'POST':
form3 = FichierUpload(request.POST or None, request.FILES, initial=initial_data)
if form3.is_valid():
form3.save()
return redirect('allfiles')
forms.py
class FichierUpload(ModelForm):
class Meta:
model = fichier4
fields = '__all__'