4π
β
I went with this work around in the save method to ignore the computed SQL Server columns. Found here: Prevent Django from updating identity column in MSSQL
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
# Hack to not save the maturity and months_open as they are computed columns
self._meta.local_fields = [f for f in self._meta.local_fields if f.name not in ('maturity', 'months_open')]
super(LocationMaster, self).save(force_insert, force_update, using, update_fields)
π€duffn
4π
I encountered this issue when a field is updated with PostgreSQL triggers, but django overwrites itβs value (in same transaction).
This is a generic class decorator that prevents sending enabled=False
fields to database (perhaps in any query, like UPDATE
).
def prevent_updating_non_editable_fields(clazz):
""" prevents sending non`editable` fields in queries """
meta = clazz._meta
meta.local_concrete_fields = [f for f in meta.local_concrete_fields if f.editable]
return clazz
@prevent_updating_editable_fields
class MyModel(models.Model):
precomputed = models.IntegerField(default=0, editable=False)
π€Taha Jahangir
- [Django]-ImportError: cannot import name <model_class>
- [Django]-Passing data to Google Charts with Django
- [Django]-Python Web Framework for Small Team
- [Django]-Django Query set to fetch a data from a database?
-3π
You should be able to accomplish this by adding editable=False
to the field.
More info here: https://docs.djangoproject.com/en/dev/ref/models/fields/#editable
π€Abid A
- [Django]-Python can I suppy username and password to os.listdir?
- [Django]-I'm trying to build site navigation using django-mptt
- [Django]-Serializer Import Error on Django Rest Framework
- [Django]-Django.db.utils.DataError: numeric field overflow β django
Source:stackexchange.com