Inside my forms.py I have a list with users you want to share the upload with.
My issue is that I don't know how to pass the information which user is currently logged in from my views.py (request.user I can access there ) to my forms.py
In the image below the first entry should vanish
forms.py
share_with = forms.MultipleChoiceField(
choices=tuple(UserProfile.objects.values_list('id', 'full_name')),
widget=forms.CheckboxSelectMultiple,
)
models.py
[...]
share_with = models.ManyToManyField(UserProfile)
[...]
views.py
@login_required
def upload_dataset(request):
form = UploadDatasetForm()
if request.method == "POST":
print('Receiving a post request')
form = UploadDatasetForm()
if form.is_valid():
print("The form is valid")
dataset_name = form.cleaned_data['dataset_name']
description = form.cleaned_data['description']
# datafiles = form.cleaned_data['datafiles']
share_with = form.cleaned_data['share_with']
instance = Datasets.objects.create(
uploaded_by=request.user.profile.full_name,
dataset_name=dataset_name,
description=description,
# datafiles = datafiles,
upvotes=0,
downvotes=0,
)
for i in share_with:
print(i)
instance.share_with.add(i)
return redirect("public:data")
print("The Dataset has been uploaded")
context = {"form": form}
return render(request, "upload_dataset.html", context)