0

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

The list

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)
Aalexander
  • 4,987
  • 3
  • 11
  • 34
  • The way you can access request in forms https://stackoverflow.com/questions/1057252/how-do-i-access-the-request-object-or-any-other-variable-in-a-forms-clean-met – Alex Paramonov Jul 27 '21 at 08:40
  • I will take a look at this two. Thanks a alot. Before I asked the question I had tried a round a bit with some SO answers but nothing seems to work. – Aalexander Jul 27 '21 at 08:43
  • @NKSM it answered my question. Thank you :) – Aalexander Jul 27 '21 at 08:49

0 Answers0