1👍
With the help of @Igor’s comment I amended my solution to this:
def archive_calc(self, object_id):
annual = InstrumentAnnual.objects.get(id = object_id)
annual_archive = InstrumentAnnualArchive()
for field in annual._meta.fields:
setattr(annual_archive, field.name, getattr(annual, field.name))
annual_archive.pk = None
annual_archive.save()
It occured to me that using pre_save
wouldn’t work as it is listening/linked to a model, not a view as I originally thought. So I placed the above code in my Update View and called it passing the id
in object_id
.
Thanks again for the help.
👤Karl
0👍
You should be using named arguments in your constructor, otherwise the first argument will be interpreted as the id
, so try:
# omitted code
e = InstrumentAnnual.objects.filter(id=stored_id)
archive = InstrumentalAnnualArchive(field_name=e.field_name, another_name=e.another_field_name, …)
archive.save()
But you could also use Django’s create
function, so:
# omitted code
e = InstrumentAnnual.objects.filter(id=stored_id)
archive = InstrumentalAnnualArchive.objects.create(field_name=e.field_name, another_name=e.another_field_name, …)
This way handles the save for you, so you don’t need to explicitly save your object.
- [Answer]-Connection of 3 Models with Foreign Key
- [Answer]-Is it possible/sensible to customize Django's auto-through tables to include other fields…through-factory?
- [Answer]-Issues with uploading images to a carousel
- [Answer]-Apache2 doesn't work after installing mod-wsgi
- [Answer]-Uploading files to directory named by the value of a Foreign key field
Source:stackexchange.com