Opened 13 years ago
Closed 10 years ago
#19193 closed New feature (duplicate)
Save only one field to database which refereced to FieldFile
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
django.db.models.fields.FieldFile:
def save(self, name, content, save=True):
....
if save:
self.instance.save(update_fields=[self.field.name])
....
or:
def save(self, name, content, save=True, **kwargs)
....
if save:
self.instance.save(**kwargs)
....
So, we get rid of re-saving fields when updating
Change History (11)
comment:1 by , 13 years ago
| Description: | modified (diff) |
|---|---|
| Triage Stage: | Unreviewed → Design decision needed |
comment:2 by , 13 years ago
This is useful when you need to update the field contains id in the name(path), which can be obtained after the save. Now we can do:
def get_files_path(instance, filename):
return os.path.join('files', str(instance.id), filename)
class MyFile(models.Model):
...
file = models.FileField(upload_to=get_files_path)
model.save() model.file.save(content.name, content)
The second query repeatedly update all fields, which is not acceptable when the data and fields to save is more.
Sorry for my English :)
comment:3 by , 13 years ago
| Triage Stage: | Design decision needed → Unreviewed |
|---|---|
| Version: | → master |
comment:4 by , 13 years ago
| Version: | master → 1.5-alpha-1 |
|---|
comment:5 by , 13 years ago
| Triage Stage: | Unreviewed → Design decision needed |
|---|
comment:6 by , 13 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
This ticket's effectively obsoleted by update_fields in Django 1.5 (https://docs.djangoproject.com/en/dev/ref/models/instances/#specifying-which-fields-to-save).
comment:7 by , 13 years ago
| Resolution: | duplicate |
|---|---|
| Status: | closed → new |
There is no such option in FieldFile.save (https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.fields.files.FieldFile.save)
comment:8 by , 13 years ago
| Version: | 1.5-alpha-1 → 1.5 |
|---|
comment:9 by , 13 years ago
| Triage Stage: | Design decision needed → Accepted |
|---|
Marking as accepted, the ability to not save all the things makes sense. Not sure if it should be the default or an option yet.
comment:10 by , 12 years ago
| Keywords: | django 1.5 removed |
|---|---|
| Version: | 1.5 → master |
As the expected release of Django 1.6, can we see this feature in it?
comment:11 by , 10 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
The reason this was originally closed is because you can do this:
obj.file.save(content.name, content, save=False) obj.save(update_fields=['file'])
This seems acceptable to me.
Option 1 is highly backwards incompatible and option 2 looks like an API bloat - passing
save=Falseand callinginstance.save()does exactly the same and avoids potential kwargs naming conflict.