send data to formset using get

Hi all
I have problem in this function :

def addrealestatepic(request,identifier):
       user = request.user
       try:
           property = Property.objects.get(identifier=identifier)
       except Property.DoesNotExist:
           raise Http404("No Property matches the given query.")
       if request.method == 'POST':
           formset = Property_pictureFormset(request.POST,request.FILES)
           if formset.is_valid():
              for form in formset: 
                name = form.cleaned_data.get("pic_name")
                img = form.cleaned_data.get("picture")
                if name != None or img != None:
                        obj = Property_picture.objects.create(
                                 property=property,
                                 pic_name = name, 
                                 picture = img,
                                 created_by=user
                                 )
                obj.save()
       else:
          queryset=Property_picture.objects.filter(property_id=property.id)
          formset = Property_pictureFormset(queryset=queryset)
return render(request,'realestate/addrealestatepic.html',{'identifier':identifier,'formset':formset,})

in get case : i send data to formset to fill template but give me error :

( TypeError at /addrealestatepic/65f0f289-1a17-41b4-b23d-7a7c081d5a87/
BaseFormSet.__init__() got an unexpected keyword argument 'queryset'

|Request Method:|GET|
|Exception Value:|BaseFormSet.__init__() got an unexpected keyword argument 'queryset'|
|Exception Location:|D:\myapps\app001\myrealestate\realestate\views.py, line 54, in addrealestatepic|
)
line 54 is : formset = Property_pictureFormset(queryset=queryset)

how i can solve this proplem plaese ???

Side Note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of modifying your original post for you.)

1 Like

Please post the definition of Property_pictureFormset.

class Property_pictureForm(forms.ModelForm):

class Meta:
    pic_name = forms.FileField()
    picture=forms.ImageField()
    model = Property_picture
    fields = ['pic_name','picture',]

Property_pictureFormset = formset_factory(Property_pictureForm, extra=2,can_delete=True)

Two things:

First, please remember to post your code between lines of ``` to prevent the forum software from reformatting it.

Two, the form field definitions belong at the class level, not in the Meta subclass.

Please post your Property_picture model.

thank you so match sir , i will do that

This is the creation of a formset, but you’re trying to use it as a modelformset. This needs to be created as a modelformset.

model is :
‘’‘class Property_picture(models.Model):
property = models.ForeignKey(Property, on_delete=models.CASCADE,related_name=‘property_pictures’)
pic_name = models.CharField(max_length=100,null=True)
picture = models.ImageField(default=‘default.jpg’, upload_to=‘aqars_pic’,null=True)
created_dt = models.DateTimeField(auto_now_add=True)
is_active=models.BooleanField(default=True)
created_by = models.ForeignKey(User,related_name=‘Property_pic_user’,on_delete=models.CASCADE,null=True) ‘’’

note that : post method save correctly

can you explain more?

What do you need explained?

Side note: For fencing your code, you need to use the backtick - `, not the apostrophe - '. Also, the line of three backticks - ```, must be lines by themselves. They must not be part of another line.

1 Like