Hi Felipe.

maybe that help you,
I use in forms.py and views.py when I need add some filter special on any
view, maybe this code can give you any idea for resolve you question:

on forms.py :

class FileForm(ModelForm):
class Meta:
model = File
fields = ['file', 'file_type', 'folder', 'file_desc', 'file_sha256']
widgets = {
'file_desc': Textarea(attrs={'class' : 'Textarea', 'rows': 4}),
'file_sha256': TextInput(attrs={'readonly' : 'true', }),
}

def __init__(self, *args, **kwargs):
owner = kwargs.pop('owner', None)
folders_owner = Folder.objects.filter(owner=owner)
file_type_owner = FileType.objects.filter(owner=owner)

super(FileForm, self).__init__(*args, **kwargs)
self.fields['file_sha256'].required = False
self.fields['file_type'] = ModelChoiceField(queryset=file_type_owner)
self.fields['folder'] = ModelChoiceField(queryset=folders_owner)

on views.py :

class FileCreateView(CreateView):
model = File
form_class = FileForm
success_message = "Documento creado con éxito."

def get_success_url(self, **kwargs):
context = super().get_context_data(**kwargs)
file = context['object']
messages.success(self.request, self.success_message)
return reverse_lazy('filesadmin:file-update', kwargs = {'pk': file.id})

def get_form_kwargs(self):
kwargs = super(FileCreateView, self).get_form_kwargs()
owner = Owner.objects.get(owner=self.request.user.id)
kwargs['owner'] = owner
return kwargs

def form_valid(self, form):
form.instance.owner = Owner.objects.get(owner=self.request.user.id)
form.instance.modified_by = self.request.user
return super().form_valid(form)


on get_form_kwargs.into kwargs I send owner(on this case user objects)
 and I received into forms.py on *def __init__*
at this moment I known who is the user and can apply my filters over this
user.

I hope can help you.

*---*
*Ángel Gabriel Morales Acosta*
Email: [email protected]


On Sat, Aug 18, 2018 at 10:46 AM Fellipe Henrique <[email protected]>
wrote:

> Thanks Jason,
>
> But, can I get current user on model manager? I need to filter with that
> info.
>
>
>
> T.·.F.·.A.·.     S+F
> *Fellipe Henrique P. Soares*
>
> e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \
> 's/(.)/chr(ord($1)-2*3)/ge'
> *Fedora Ambassador: https://fedoraproject.org/wiki/User:Fellipeh
> <https://fedoraproject.org/wiki/User:Fellipeh>*
> *Blog: *http:www.fellipeh.eti.br
> *GitHub: https://github.com/fellipeh <https://github.com/fellipeh>*
> *Twitter: @fh_bash*
>
>
> On Sat, Aug 18, 2018 at 9:10 AM Jason <[email protected]> wrote:
>
>> make a custom model manager for that model.
>>
>> https://docs.djangoproject.com/en/2.1/topics/db/managers/#custom-managers
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/63e2765e-96a6-4283-ba6b-7c8ac5f25cb3%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/63e2765e-96a6-4283-ba6b-7c8ac5f25cb3%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAF1jwZEoRS0%3DqOmoOcViUH_HseiLTe1670p6i%3DMQYBxG%2BYeKjA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAF1jwZEoRS0%3DqOmoOcViUH_HseiLTe1670p6i%3DMQYBxG%2BYeKjA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPF44MC3z2GRnbVdpvV088ZATX%2BS60PbOBLAQ1pxmgnOB3dBKw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to