1👍
✅
In order to remain as DRY
as possible, you could have an abstract mock class deriving your model:
class A(models.Model):
# fields'n'stuff
class TransientA(A):
def save(*args, **kwargs):
pass # avoid exceptions if called
class Meta:
abstract = True # no table created
Now, even if you call save
on it anywhere (even in methods inherited from A
), you’ll be shooting blanks.
3👍
You’ll need both:
abstract = True
is useful if inheritants still should be concrete models, so that no table should be created just for the parent class. It allows you to opt out of multi-table inheritance, and instead have the shared attributes duplicated to inheritants tables instead (abstract base inheritance).
managed = False
is useful if the inheriting class should never be persisted at all. Django migrations and fixtures won’t generate any database table for this.
class TransientModel(models.Model):
"""Inherit from this class to use django constructors and serialization but no database management"""
def save(*args, **kwargs):
pass # avoid exceptions if called
class Meta:
abstract = True # no table for this class
managed = False # no database management
class Brutto(TransientModel):
"""This is not persisted. No table app_brutto"""
#do more things here
pass
- [Django]-Problem with django urls which have a hyphen in them
- [Django]-How to eliminate inefficiency of django query in a loop?
- [Django]-Django unique_together on primary key
Source:stackexchange.com