In the django admin, I have an inline that I want to have the viewing user filled in automatically. During the clean
function, it fills in the created_by
field with request.user
. The problem is that since the created_by
field is excluded by the form, the value that gets inserted into cleaned_fields
gets ignored apparently. How can I do this? I want the widget t not be displayed at all.
class NoteInline(admin.TabularInline):
model = Note
extra = 1
can_delete = False
def get_formset(self, request, obj=None, **kwargs):
"""
Generate a form with the viewing CSA filled in automatically
"""
class NoteForm(forms.ModelForm):
def clean(self):
self.cleaned_data['created_by'] = request.user
return self.cleaned_data
class Meta:
exclude = ('created_by', )
model = Note
widgets = {'note': forms.TextInput(attrs={'style': "width:80%"})}
return forms.models.inlineformset_factory(UserProfile, Note,
extra=self.extra,
form=NoteForm,
can_delete=self.can_delete)