1👍
The assignments are late. In the __init__()
of ModelForm
, or in your code: the form = UploadForm(request.POST, request.FILES)
line, an instance has been created and populated to initial
of the form. Later modification upon the instance will not affect the value of form.initial
.
Also, if you want to fill in some fields automatically in backend, don’t render them to user.
Thus, yes, you could make them blank=True
or exclude them from the form then follow the suggested way.
2👍
Try this:
data = request.POST.copy()
data['company_id'] = r_user.company.id
data['uploaded_by_id'] = r_user.id
data['bucket_id'] = r_user.company.s3_bucket_id
form = UploadForm(data, request.FILES)
if form.is_valid():
form_object = form.save()
This way you are setting the data before creating the form.
- [Django]-Applying a Django FilterSet to an Annotated QuerySet?
- [Django]-How to filter using an expression in Django
- [Django]-Can't "activate" virtualenv
0👍
Actually you do here a couple of strange things.
First:
Why do you assign to company_id
, uploaded_by_id
and bucket_id
? However yout got those fields in your model – it’s names of columns in table created for this model – you should use ORM features directly here e.g. form.instance.company = r_user.company
.
Second:
Form validation is applied to form.fields not to it’s instance attribute, and you can’t change them once form is created and bounded to supplied values.
The recommended way to add some fields values to model instance created by form in Django (as I always thought) is to create custom ModelForm as you do and in Meta
class specify all fields you want allow user to edit. For your case:
class UploadForm(ModelForm):
class Meta:
model = UploadedFile
fields = ('file', 'slug')
Then instead of calling form_object = form.save()
you call form_object = form.save(commit=False)
. Now you got your model instance with all fields from form filled already so you can add what you wish: e.g. form_object.company = r_user.company
. Finally don’t forget to call save()
on model instance itself to store it in your database.
This solution it described in Django documentation here
- [Django]-Using the URLconf defined in first_project.urls, Django tried these URL patterns, in this order:
- [Django]-Pandas- grouping and aggregating consecutive rows with same value in column
- [Django]-Create HTML button to run Python Script
- [Django]-Django transforming an old url to new syntax