0

i am saving a form with a filefield, and saying upload_to to a user_path from the userprofile. I do not know how to write the view for the form

models.py


def nice_user_folder_upload(instance, filename):
    extension = filename.split(".")[-1]
    return (
        f"{instance.UserProfile.Assigned_Group}/{filename}"
    )

class Metadataform(models.Model):


    id = models.AutoField(primary_key=True)
    Authors_Name = models.CharField(max_length=500, blank=True, null=True)
    Document = models.FileField(upload_to=nice_user_folder_upload)

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    Assigned_Group= models.CharField(max_length=500, choices=Group_choices, default='Please Select')

    def __str__(self):
        return self.user.username

views.py

def Metadata_submission(request):


        Authors_Name = request.POST["Authors_Name"]
          if request.method == 'POST':
                form = Fileuploadform(request.POST, request.FILES)
                if form.is_valid():
                        form.save()
                   return render(request, "home.html")

        else:
                form = Fileuploadform()
# forms.py                              

class Fileuploadform(forms.ModelForm):
    class Meta:
        model = Metadataform
        fields = ['Authors_Name','Affliations','Dataset_Creation_Date','Publication_Ref','Embargo_Time','DataGeneration_Type','Methods','Instruments','Software','Models','Device','Configuration','Precursor','Data_Type','Variables','Error_Estimation','Document']


class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('Assigned_Group',)

i am getting an AttributeError at /Metadata_submission/ 'Metadataform' object has no attribute 'UserProfile'

user11603936
  • 69
  • 1
  • 7
  • Can i please get some guidance? – user11603936 Oct 08 '19 at 09:58
  • Can you post the HTML form that you are using in your view? – Al Amin Oct 08 '19 at 10:28
  • have added the html file to my question – user11603936 Oct 08 '19 at 10:39
  • Where is the ending tag for your form? Is there any other fields you have on the form? – Al Amin Oct 08 '19 at 10:48
  • yes i have few more fields in the form, but why do you want to see the ending tag? if you could please explain i can update the changes accordingly – user11603936 Oct 08 '19 at 10:51
  • First, I need to see what other fields do you have. By the look of it, you didn't give your Fileuploadform here. Maybe you are trying to save in two models with the same form, just a hunch. – Al Amin Oct 08 '19 at 10:55
  • i have added the forms.py for your ref, there you can see the list of other fields i have in the form – user11603936 Oct 08 '19 at 11:00
  • Your "upload_to" path uses `instance.UserProfile.Assigned_Group`. That's wrong and causing the error. What is the relationship between `Metadataform` and `UserProfile`? Note `instance` is the instance of `Metadataform`. – dirkgroten Oct 08 '19 at 11:55
  • i want to add the uploaded files to the group that the user is assigned to. that is why i am connecting the userprofile and metadataform – user11603936 Oct 08 '19 at 17:56

1 Answers1

1

The problem here I think is you have so many fields here that might not be associated with your Metadataform model (Maybe you haven't posted it in full). I think you should consider reading doc. By the look of it, you are trying to add UserProfile somewhere on your form, which is causing the error.

Al Amin
  • 889
  • 7
  • 12