How to make a Django Model form Readonly? - django

I have a django form name "SampleForm". Which i use to take input from user. Now i want to use same form to show this information to user on a different page. But form is editable I want to make the form read only. Is there any way to make whole form Readonly ?

pseudo-code (not tested):
class ReadOnlyFormMixin(ModelForm):
def __init__(self, *args, **kwargs):
super(ReadOnlyFormMixin, self).__init__(*args, **kwargs)
for key in self.fields.keys():
self.fields[key].widget.attrs['readonly'] = True
def save(self, *args, **kwargs):
# do not do anything
pass
class SampleReadOnlyForm(ReadOnlyFormMixin, SampleForm):
pass

class SampleForm(ModelForm):
def __init__(self, *args, **kwargs):
super(SampleForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.pk:
for field in self.fields.keys():
self.fields[field].widget.attrs['readonly'] = True
Just this should make the entire form readonly whenever an instance is available for the form.

Working Code
class ReadOnlySampleForm(SampleForm):
def __init__(self, *args, **kwargs):
super(SampleForm, self).__init__(*args, **kwargs)
for key in self.fields.keys():
self.fields[key].widget.attrs['readonly'] = True

in Django 4.1, override the get_context_data with
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(object_list=None, **kwargs)
form = AddressForm(instance=self.get_object())
for name, field in form.fields.items():
field.disabled = True
context["form"] = form
return context

Related

AreYouHuman and django

I am trying to get http://areyouahuman.com/ capcha working on my site
I found
https://gist.github.com/klanestro/9572114
I subclassed it
class Turtle_Form(forms.ModelForm,AreYouHumanForm):
''' This is code to make sure the User automatically saves the
user to the database in context.
'''
def save(self, *args, **kwargs):
kwargs['commit']=False
obj = super(Turtle_Form, self).save(*args, **kwargs)
if self.request:
obj.user = self.request.user
obj.save()
return obj #<--- Return saved object to caller.
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
return super(Turtle_Form, self).__init__(*args, **kwargs)
and
class OfertoCreateForm(Turtle_Form):
class Meta:
model = Oferto
fields = ( "name",
"description",
"tags",
"time",
"requirements",
"location",
"image",
'AreYouHumanForm',
)
but nothing comes up on the form, no errors nothing.
The field in that gist is called session_secret, which is the name you should be using in the fields tuple.

Display Selection from Django choicefield?

Is there a way to display the selection in Django choicefield? For example, I would like to
display the selected choice in "S". Is there an easy way to do this(build-in function)?
S_CHOICES=(('A','A'),('B','B'),('Other','Other'))
S = forms.ChoiceField(choices=select_CHOICES, initial='A')
def __init__(self, *args, **kwargs):
super(YourForm, self).__init__(*args, **kwargs)
self['S'].value()
print(S)
You can do it in form's __init__:
class YourForm(forms.Form):
S_CHOICES=(('A','A'),('B','B'),('Other','Other'))
S = forms.ChoiceField(choices=select_CHOICES)
def __init__(self, *args, **kwargs):
super(YourForm, self).__init__(*args, **kwargs)
self.fields['S'].initial = 'A'

Passing variables when instantiating a form class using FormWizard

I have a simple form which uses a queryset that is set dynamically:
class FooForm(forms.Form):
bar = forms.ModelChoiceField(queryset = Bar.objects.none())
def __init__(self, queryset=None, *args, **kwargs):
super(FooForm, self).__init__(*args, **kwargs)
self.fields['bar'].queryset = queryset
I'd like to use this as one of my forms in a FormWizard, but I can't figure out how I can get FormWizard to pass on the queryset. Is this possible at all?
I think you could override the "get_form" method for that particular wizard
class MyWizard(FormWizard):
def __init__(self, *args, **kwargs):
self.querysets = kwargs.pop('querysets', None)
super(self, MyWizard).__init__(*args, **kwargs)
def get_form(self, step, data=None, *args, **kwargs):
return self.form_list[step](data, prefix=self.prefix_for_step(step), initial=self.initial.get(step, None), queryset=self.querysets.get(step, None))
def done(self, *args, **kwargs): pass

django: how to access current request user in ModelForm?

In my implementation of ModelForm, I would like to perform different types of validation checks based on whether current user is superuser. How can I access the current request user?
If you're using Class Based Views (CBVs) then passing an extra argument in the form constructor (e.g. in get_forms_class) or in form_class will not work, as <form> object is not callable will be shown.
The solution for CBVs is to use get_form_kwargs(), e.g.:
views.py:
class MyUpdateView(UpdateView):
model = MyModel
form_class = MyForm
# Sending user object to the form, to verify which fields to display/remove (depending on group)
def get_form_kwargs(self):
kwargs = super(MyUpdateView, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
forms.py:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user') # To get request.user. Do not use kwargs.pop('user', None) due to potential security hole
super(MyForm, self).__init__(*args, **kwargs)
# If the user does not belong to a certain group, remove the field
if not self.user.groups.filter(name__iexact='mygroup').exists():
del self.fields['confidential']
you can pass the user object as an extra argument in the form constructor.
e.g.
f = MyForm(user=request.user)
and the constructor will look like:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user',None)
super(MyForm, self).__init__(*args, **kwargs)
and then use user in the clean_XX forms as you wish
My small addition,
I had a requirement where one of the model choice fields of the form is dependent on the request.user, and it took a while to take my head around.
The idea is that
you need to have a __init__ method in the model form class,
and you access the request or other parameters from the arguments of the __init__ method,
then you need to call the super constructor to new up the form class
and then you set the queryset of the required field
code sample
class CsvUploadForm(forms.Form):
def __init__(self, *args, **kwargs):
user = kwargs.pop('user')
super(CsvUploadForm, self).__init__(*args, **kwargs)
self.fields['lists'].queryset = List.objects.filter(user=user)
lists = forms.ModelChoiceField(queryset=None, widget=forms.Select, required=True)
as you can see, the lists variable is dependent on the current user, which is available via request object, so we set the queryset of the field as null, and its assigned dynamically from the constructor later.
Take a look into the order of the statements in the above code
you can pass the user variable like this from the view file
form = CsvUploadForm(user=request.user)
or with other POST, FILE data like below
form = CsvUploadForm(request.POST, request.FILES, user=request.user)
You may reference the user object using the instance attribute within the instance it self.
Ex; self.instance.user
class StatusForm(ModelForm):
# def __init__(self, *args, **kwargs):
# self.user = kwargs.pop('user', None)
# super(StatusForm, self).__init__(*args, **kwargs)
class Meta:
model = Status
fields = [
'user',
'content',
'image'
]
def clean_content(self):
content = self.cleaned_data.get("content", None)
if len(content) > 240:
raise ValidationError(f"Hey {self.instance.user.username}, the content is too long")
return content
This worked for me, when I am not sending form in context explicitly in get_context_data:
views.py
class MyView(FormView):
model = MyModel
form_class = MyForm
def get_form_kwargs(self):
kwargs = super(MyView, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
form.py
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(MyForm, self).__init__(*args, **kwargs)
if not self.user.groups.filter(name__iexact='t1_group').exists():
del self.fields['test_obj']
When sending form explicitly in get_context_data we can use and this is forms.Form :
views.py
class MyView(FormView):
model = MyModel
form_class = MyForm
def get_context_data(self, **kwargs):
context = super(MyView, self).get_context_data(**kwargs)
context['form'] = self.form_class(self.request.user)
return context
forms.py
class MyForm(forms.Form):
def __init__(self, user,*args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
if not user.groups.filter(name__iexact='t1_group').exists():
del self.fields['test_obj']

Django Admin: Getting request in ModelForm's constructor

The below code removes certain values from a drop down menu.
It works fine but I want to remove the value if the user lacks certain permissions.
How can I access request.user in the ModelForm's constructor? Or is there a better way to accomplish what I am trying to do?
class AnnouncementModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(AnnouncementModelForm, self).__init__(*args, **kwargs)
self.fields["category"].queryset = AnnouncementCategory.objects.filter(can_post=True)
How can I access request.user in the ModelForm's constructor?
To use it in the Form constructor, just pass the request to it.
class AnnouncementModelForm(forms.ModelForm):
def __init__(self, request, *args, **kwargs):
super(AnnouncementModelForm, self).__init__(*args, **kwargs)
qs = request.user.foreignkeytable__set.all()
self.fields["category"].queryset = qs
Ok here is how I solved it:
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "category" and not request.user.has_perm('can_post_to_all'):
kwargs["queryset"] = AnnouncementCategory.objects.filter(can_post=True)
return db_field.formfield(**kwargs)
return super(AnnouncementAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)