How to make optionally read-only fields in django forms? - django

I have a read-only field in a django form that I sometimes want to edit.
I only want the right user with the right permissions to edit the field. In most cases the field is locked, but an admin could edit this.
Using the init function, I am able to make the field read-only or not, but not optionally read-only. I also tried passing an optional argument to StudentForm.init but that turned much more difficult that I expected.
Is there a proper way to do accomplish this?
models.py
class Student():
# is already assigned, but needs to be unique
# only privelidged user should change.
student_id = models.CharField(max_length=20, primary_key=True)
last_name = models.CharField(max_length=30)
first_name = models.CharField(max_length=30)
# ... other fields ...
forms.py
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = ('student_id', 'last_name', 'first_name',
# ... other fields ...
def __init__(self, *args, **kwargs):
super(StudentForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance:
self.fields['student_id'].widget.attrs['readonly'] = True
views.py
def new_student_view(request):
form = StudentForm()
# Test for user privelige, and disable
form.fields['student_id'].widget.attrs['readonly'] = False
c = {'form':form}
return render_to_response('app/edit_student.html', c, context_instance=RequestContext(request))

Is that what you are looking for? By modifying your code a little bit:
forms.py
class StudentForm(forms.ModelForm):
READONLY_FIELDS = ('student_id', 'last_name')
class Meta:
model = Student
fields = ('student_id', 'last_name', 'first_name')
def __init__(self, readonly_form=False, *args, **kwargs):
super(StudentForm, self).__init__(*args, **kwargs)
if readonly_form:
for field in self.READONLY_FIELDS:
self.fields[field].widget.attrs['readonly'] = True
views.py
def new_student_view(request):
if request.user.is_staff:
form = StudentForm()
else:
form = StudentForm(readonly_form=True)
extra_context = {'form': form}
return render_to_response('forms_cases/edit_student.html', extra_context, context_instance=RequestContext(request))
So the thing is to check permissions on the views level, and then to pass argument to your form when it is initialized. Now if staff/admin is logged in, fields will be writeable. If not, only fields from class constant will be changed to read only.

It would be pretty easy to use the admin for any field editing and just render the student id in the page template.
I'm not sure if this answers your questions though.

Related

Cannot assign "": "" must be a '' instance

I am trying to make a registration key in the UserModel where the key field in the registration form is a foreign key to another model called RegistrationKey. I have made two posts about this topic earlier without any success, however has a few things changed in my code which makes those previous posts irrelevant. In the form field, the field for the key is a CharField as I can not display the keys for the users due to safety.
These are the two previous posts:
Save user input which is a string as object in db ,
Textinput with ModelChoiceField
These are my two models.
class RegistrationKey(models.Model):
key = models.CharField(max_length=30)
def __str__(self):
return self.key
class User(AbstractUser):
key = models.ForeignKey(RegistrationKey, on_delete=models.CASCADE, null=True, blank=True)
Since my latest posts have I created a class based view, which looks like this:
class RegisterPage(CreateView):
form_class = MyUserCreationForm
def form_valid(self, form):
key = form.cleaned_data['key']
try:
keyobject = RegistrationKey.objects.get(key=key)
form.instance.key = keyobject
return super().form_valid(form)
except RegistrationKey.DoesNotExist:
form.add_error('key', 'error')
return super().form_invalid(form)
When I try and pass in the value Admin which is an object in the RegistrationKey model I get the following error:
'Cannot assign "'Admin'": "User.key" must be a "RegistrationKey" instance.'
I don't know how to solve this, how can this string that the user inputs be assigned to the db?
Edit
Here are my form
class MyUserCreationForm(UserCreationForm):
key = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Key'}), label='')
email = forms.EmailField(widget=forms.EmailInput(attrs={'class':'form-control', 'placeholder':'Email'}), label='')
class Meta:
model = User
fields = ('key', 'email', 'password1', 'password2')
def __init__(self, *args, **kwargs):
super(MyUserCreationForm, self).__init__(*args, **kwargs)
self.fields['password1'].widget.attrs['class'] = 'form-control'
self.fields['password1'].widget.attrs['placeholder'] = 'Password'
self.fields['password1'].label=''
self.fields['password2'].widget.attrs['class'] = 'form-control'
self.fields['password2'].widget.attrs['placeholder'] = 'Confirm Password'
self.fields['password2'].label=''
for fieldname in ['password1', 'password2']:
self.fields[fieldname].help_text = None
You better move the logic to obtain the item to the form, where it belongs. So with:
from django.core.exceptions import ValidationError
class MyUserCreationForm(UserCreationForm):
# …
def clean_key(self):
key = self.cleaned_data['key']
try:
return RegistrationKey.objects.get(key=key)
except RegistrationKey.DoesNotExist:
raise ValidationError('The key is not valid.')
That should be sufficient. You should not override the .form_valid(…) method.

Manually filling disabled fields of Django ModelForm

I am facing the following scenario: I have a Django model class called Contact, which looks something like:
class Contact(models.Model):
first_name = models.CharField(max_length=70)
last_name = models.CharField(max_length=70)
company = models.ForeignKey(Company) // should be disabled in user-facing forms
organizations = models.ManyToManyField(Organization) // should be disabled and hidden in user-facing forms
// some other fields not relevant to this question
Both users of the app and administrators should be able to create objects of type Contact and store it in the database. However, for a user this should be restricted in the way that he cannot freely chose the company field of a Contact object. For this, I have created a base ModelForm called ContactForm, intended to be used by administrators, and a restricted user-facing child class called RestrictedContactForm. The code looks as follows:
class ContactForm(forms.modelForm):
class Meta:
model = Contact
fields = ['first_name', 'last_name', 'company', 'organizations']
class RestrictedContactForm(ContactForm):
class Meta(ContactForm.Meta):
widgets = {'organizations': forms.HiddenInput()}
def __init__(self, *args, **kwargs):
super(RestrictedContactForm, self).__init__(*args, **kwargs)
// Maybe populate company and organization here somehow?
self.fields['company'].disabled = True
self.fields['organization'].disabled = True
The RestrictedContactForm is rendered to the user once he decides to create a new contact. Clearly, as both the company and organization fields are mandatory, they need to be manually injected somehow. It is exactly here where my problem lies: I haven't managed to populate these fields by hand.
Below you can find an outline of the view function implementing the logic of a user initiated creation.
def create_contact(request, company_pk):
company = Company.objects.get(pk=company_pk)
organization = Organization.objects.get(...)
if request.method == 'POST':
// Add company.pk and organization.pk to POST here?
// Pass data dictionary manually populated from POST and
// with company.pl and organization.pk to constructor?
contact_form = RestrictedContactForm(request.POST, request.FILES)
// Add company.pk and organization.pk to contact_form.data
// here (after making it mutable)?
if contact_form.is_valid():
contact_form.save()
return redirect(...)
return render(...)
contact_form = ContactForm(initial={'company': company, 'organizations': organization})
I have already tried every suggestion appearing in the comments above. The form simply never validates. My question hence is, what would be the correct way of doing this? Moreover, is the approach outlined at least conceptually right?
The project uses Django 1.9.
If the company and organization fields are not changeable by the user, then they should not be included in the fields list at all in RestrictedContactForm.
What you can do instead is pass the known values for organization and company into the constructor of the form, and then assign them to the object before you actually create it in the database.
class RestrictedContactForm(ContactForm):
class Meta(ContactForm.Meta):
fields = ['first_name', 'last_name', ]
def __init__(self, company, organization, *args, **kwargs):
super(RestrictedContactForm, self).__init__(*args, **kwargs)
self.company = company
self.organization = organization
def save(self, commit=True):
instance = super(RestrictedContactForm, self).save(commit=False)
if not instance.pk:
instance.company = self.company
instance.organization = self.organization
if commit:
instance.save()
return instance
def create_contact(request, company_pk):
# ...
if request.method == 'POST':
company = Company.objects.get(pk=company_pk)
organization = company.organization
contact_form = RestrictedContactForm(company, organization, request.POST, request.FILES)
# ...
# ...
I've always done this using the form_valid method. In this case, in the form_valid method of the child form:
def form_valid(self, form):
form.instance.company = foo
form.instance.organisation = bar
return super().form_valid(form)
This populates the missing field, and then saves the form.

Django Many-to-many limiting choices on a formset so forced removal of inactive?

The requirement is a many-to-many relationship between users and projects.
Both User and Project model have an is_active attribute.
There is an inline formset when editing the User and Project with an updateview.
The many-to-many field is controlled through an intermediate table.
On User model:
projects = models.ManyToManyField(
Project,
through=ProjectMembership
)
On Project model:
users = models.ManyToManyField(
settings.AUTH_USER_MODEL,
through='ProjectMembership'
)
On ProjectMembership intermediate model I am setting the limit_choices_to:
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
limit_choices_to={'is_active': True},
)
project = models.ForeignKey(
Project,
on_delete=models.PROTECT,
limit_choices_to={'is_active': True},
)
On the formset a user can only be assigned to a new project and the same the other way around.
The problem comes in with existing project that were made inactive.
So you can save active projects to a user:
But when you change the is_active status of Stackoverflow Answering to False:
And then when you try to save it forces you to delete the row:
Ideally I want inactive project to be disabled or not visible at all.
Which would mean overriding the get_initial_data or initial queryset.
Also they wouldn't be validated with the clean method. How can I specifically fix this?
The Formset:
class ProjectMembershipForm(forms.ModelForm):
class Meta:
model = ProjectMembership
fields = (
'user',
'project',
'is_project_manager',
)
ProjectMembershipFormSet = forms.inlineformset_factory(
Project,
ProjectMembership,
form=ProjectMembershipForm,
extra=1,
can_delete=True
)
The UserUpdateView:
class UserUpdateView(UpdateView):
model = get_user_model()
form_class = forms.UserUpdateForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.request.POST:
context['projectmembership_formset'] = forms.UserProjectMembershipFormSet(
self.request.POST,
instance=self.object
)
else:
context['projectmembership_formset'] = forms.UserProjectMembershipFormSet(
instance=self.object
)
return context
def form_valid(self, form):
'''Handle saving of the project membership formset
'''
context = self.get_context_data()
project_memberships = context['projectmembership_formset']
if project_memberships.is_valid():
self.object = form.save()
project_memberships.instance = self.object
project_memberships.save()
return super().form_valid(form)
else:
return self.render_to_response(self.get_context_data(form=form))
How are you doing your form? Class-based view? UpdateView? FormView? If you post your view-code I'll be able to give a much better answer.
If you're using any sort of form-view, the solution should be pretty easy. You'll just need to pass the desired queryset into the formfield as an argument.
class AddProjectToUserForm(forms.form):
projects = forms.ModelChoiceField(queryset=Partner.objects.filter(is_active=True))
EDIT: for a modelForm try this:
class ProjectMembershipForm(forms.ModelForm):
class Meta:
model = ProjectMembership
fields = (
'user',
'project',
'is_project_manager',
)
def __init__(self, *args, **kwargs):
super(ProjectMembershipForm, self).__init__(*args, **kwargs)
self.fields['project'].queryset = Project.objects.filter(is_active=True)
self.fields['user'].queryset = User.objects.filter(is_active=True)
self.fields holds references to all the fields on a form, and lets you override properties like disabled before sending them off to the browser. If you want to see more about that, put an import ipdb; ipdb.set_trace() into the init method and wander around self.fields.
I hope that helps.
EDIT TWO:
In that case, you should try other modifications to the field in init. What about setting the field as disabled if its "initial" data is illegal?
def __init__(self, *args, **kwargs):
super(ProjectMembershipForm, self).__init__(*args, **kwargs)
project = self.fields['project']
if projcet.initial and project.initial not in project.queryset:
project.disabled = True
I'm not positive that will work, but something like it should do the trick.

Django: How to remove fields from the admin form for specific users?

My admin looks like this (with no exclude variable):
class MovieAdmin(models.ModelAdmin)
fields = ('name', 'slug', 'imdb_link', 'start', 'finish', 'added_by')
list_display = ('name', 'finish', 'added_by')
list_filter = ('finish',)
ordering = ('-finish',)
prepopulated_fields = {'slug': ('name',)}
form = MovieAdminForm
def get_form(self, request, obj=None, **kwargs):
form = super(MovieAdmin, self).get_form(request, obj, **kwargs)
form.current_user = request.user
return form
admin.site.register(Movie, MovieAdmin)
The form:
class MovieAdminForm(forms.ModelForm):
class Meta:
model = Movie
def save(self, commit=False):
instance = super(MovieAdminForm, self).save(commit=commit)
if not instance.pk and not self.current_user.is_superuser:
if not self.current_user.profile.is_manager:
instance.added_by = self.current_user.profile
instance.save()
return instance
I'm trying to remove the added_by field for users since I'd prefer to populate that from the session. I've tried methods from the following:
Django admin - remove field if editing an object
Remove fields from ModelForm
http://www.mdgart.com/2010/04/08/django-admin-how-to-hide-fields-in-a-form-for-certain-users-that-are-not-superusers/
However with each one I get: KeyError while rendering: Key 'added_by' not found in Form. It seems I need to remove the field earlier in the form rendering process but I'm stuck on where to do this.
So how can I exclude the added_by field for normal users?
You're probably getting that error when list_display is evaluated. You can't show a field that's excluded. The version with added_by removed also needs a corresponding list_display.
def get_form(self, request, obj=None, **kwargs):
current_user = request.user
if not current_user.profile.is_manager:
self.exclude = ('added_by',)
self.list_display = ('name', 'finish')
form = super(MovieAdmin, self).get_form(request, obj, **kwargs)
form.current_user = current_user
return form

field added dynamically to a ModelForm at __init__ does not save

I'm using Django profiles and was inspired by James Bennett to create a dynamic form (http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/ )
What I need is a company field that only shows up on my user profile form when the user_type is 'pro'.
Basically my model and form look like:
class UserProfile(models.Model):
user_type = models.CharField(...
company_name = models.CharField(...
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = ('company_name',)
And I add the company_name field in init like James Bennett showed:
def __init__(self, *args, **kwargs):
super(UserProfileForm, self).__init__(*args,**kwargs)
if (self.instance.pk is None) or (self.instance.user_type == 'pro'):
self.fields['company_name'] = forms.CharField(...
The problem is that, when I try to save() an instance of UserProfileForm, the field 'company_name' is not saved...
I have gone around this by calling the field explicitly in the save() method:
def save(self, commit=True):
upf = super(UserProfileForm, self).save(commit=False)
if 'company_name' in self.fields:
upf.company_name = self.cleaned_data['company_name']
if commit:
upf.save()
return upf
But I am not happy with this solution (what if there was more fields ? what with Django's beauty ? etc.). It kept me up at night trying to make the modelform aware of the new company_name field at init .
And that's the story of how I ended up on stackoverflow posting this...
I would remove this logic from form and move it to factory. If your logic is in factory, you can have two forms:
UserProfileForm
ProUserProfileForm
ProUserProfileForm inherits from UserProfileForm and changes only "exclude" constant.
You will have then following factory:
def user_profile_form_factory(*args, instance=None, **kwargs):
if (self.instance.pk is None) or (self.instance.user_type == 'pro'):
cls = ProUserProfileForm
else:
cls = UserProfileForm
return cls(*args, instance, **kwargs)
It seems I found a solution:
def AccountFormCreator(p_fields):
class AccountForm(forms.ModelForm):
class Meta:
model = User
fields = p_fields
widgets = {
'photo': ImageWidget()
}
return AccountForm
#...
AccountForm = AccountFormCreator( ('email', 'first_name', 'last_name', 'photo', 'region') )
if request.POST.get('acforms', False):
acform = AccountForm(request.POST, request.FILES, instance=request.u)
if acform.is_valid():
u = acform.save()
u.save()
ac_saved = True
else:
acform = AccountForm(instance = request.u)
When are you expecting the user_type property to be set? This seems like something that should be handled by javascript rather than trying to do funny things with the model form.
If you want the company_name field to appear on the client after they've designated themselves as a pro, then you can 'unhide' the field using javascript.
If instead, they've already been designated a pro user, then use another form that includes the company_name field. You can sub-class the original model form in the following manner.
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = ('company_name',)
class UserProfileProForm(UserProfileForm):
class Meta:
exclude = None # or maybe tuple() you should test it
Then in your view, you can decide which form to render:
def display_profile_view(request):
if user.get_profile().user_type == 'Pro':
display_form = UserProfileProForm()
else:
display_form = UserProfileForm()
return render_to_response('profile.html', {'form':display_form}, request_context=...)
This would be the preferred way to do it in my opinion. It doesn't rely on anything fancy. There is very little code duplication. It is clear, and expected.
Edit: (The below proposed solution does NOT work)
You could try changing the exclude of the meta class, and hope that it uses the instances version of exclude when trying to determine whether to include the field or not. Given an instance of a form:
def __init__(self, *args, **kwargs):
if self.instance.user_type == 'pro':
self._meta.exclude = None
Not sure if that will work or not. I believe that the _meta field is what is used after instantiation, but I haven't verified this. If it doesn't work, try reversing the situation.
def __init__(self, *args, **kwargs):
if self.instance.user_type != 'pro':
self._meta.exclude = ('company_name',)
And remove the exclude fields altogether in the model form declaration. The reason I mention this alternative, is because it looks like the meta class (python sense of Meta Class) will exclude the field even before the __init__ function is called. But if you declare the field to be excluded afterwards, it will exist but not be rendered.. maybe. I'm not 100% with my python Meta Class knowledge. Best of luck.
What about removing exclude = ('company_name',) from Meta class? I'd think that it is the reason why save() doesn't save company_name field