Django filter the queryset of ModelChoiceField -


i'm been using form field called modelchoicefield , querying objects it's not extactly intended use for.

 class pictureform(forms.modelform):     whiteboard = forms.modelchoicefield(queryset=whiteboard.objects.all()) 

i'm been trying use modelchoicefield query whiteboard objects belong particular user

whiteboard = forms.modelchoicefield(queryset=whiteboard.objects.filter(user=request.user)) 

but discovered request not pass modelform .i'm been searching through various solution , 1 solution override forms's init()

this closest question relate problem how use request in modelform in django

this solution .if solution override queryset @ views . how create modelchoicefield without queryset @ forms.py

in case , want filter whiteboards users. how it?

parts of module

class whiteboard(models.model):      category =models.charfield(max_length=30,choices=category)     user = models.foreignkey(user)     name = models.charfield(max_length=100)     picture = models.onetoonefield('picture',related_name='picture',blank=true,null=true)     def __unicode__(self):         return self.name  class picture(models.model):     user = models.foreignkey(user)     whiteboard = models.foreignkey(whiteboard,blank=false,null=false,related_name='board')     image = models.filefield(upload_to="images/",blank=true)     description = models.textfield()     is_primary = models.booleanfield(default=false)      def __unicode__(self):         return self.description 

my views.py

def picturecreator(request): if not request.user.is_authenticated():     return httpresponseredirect(reverse('world:loginrequest'))  if request.method == "post":     form = pictureform(request.post , request.files)     if form.is_valid():         picture = picture(user=request.user)         image = request.files.get('image')         if image:                             picture.image = form.cleaned_data['image']                     description = form.cleaned_data['description']         if description:                             picture.description = form.cleaned_data['description']          if board:                             picture.board = form.cleaned_data['board']         picture.save()         board = whiteboard.objects.get(whiteboard=picture.board)         the_id = board.id     return httpresponseredirect(reverse('world:boat', kwargs={'animal_id': the_id }))     return render(request,'picture.html',{'form':pictureform()}) 

picture.html

<form method="post" enctype="multipart/form-data">     {% csrf_token %} {{ form.as_p }} <input type = "submit" value= "add picture" />  </form> 

pass user in init in form in view must pass user value pictureform(request.user)

class pictureform(forms.modelform):     class meta:         model = picture      def __init__(self, user, *args, **kwargs):         super(pictureform, self).__init__(*args, **kwargs)         self.fields['whiteboard'].queryset = whiteboard.objects.filter(user=user)   def picturecreator(request):     if not request.user.is_authenticated():         return httpresponseredirect(reverse('world:loginrequest'))      if request.method == "post":         form = pictureform(request.user, request.post, request.files)         if form.is_valid():             picture = picture(user=request.user)              image = request.files.get('image')             if image:                 picture.image = form.cleaned_data['image']              description = form.cleaned_data['description']             if description:                 picture.description = form.cleaned_data['description']              if board:                 picture.board = form.cleaned_data['board']             picture.save()              board = whiteboard.objects.get(whiteboard=picture.board)             the_id = board.id         return httpresponseredirect(reverse('world:boat', kwargs={'animal_id': the_id }))        return render(request,'picture.html',{'form':pictureform(request.user)}) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -