Professional Documents
Culture Documents
name mangling - declaring a class variable as __age will prevent direct access from
outside
- still, they can be accessed as follows: sample_instance._SampleClass__value
- Name mangling is particularly useful when you want to ensure that a given
attribute or method won’t get accidentally overwritten. It’s a way to avoid naming
conflicts between classes or subclasses. It’s also useful to prevent subclasses
from overriding methods that have been optimized for better performance.
By default, Django adds a Manager with the name objects to every Django model
class. However, if you want to use objects as a field name, or if you want to use a
name other than objects for the Manager, you can rename it on a per-model basis
author = Author.objects.annotate(age_as_float=Cast("age",
output_field=FloatField())).get()
- Coalesce takes a list of fields and returns the first with a non-null value
Author.objects.create(name="Margaret Smith", goes_by="Maggie")
author = Author.objects.annotate(screen_name=Coalesce("alias", "goes_by",
"name")).get() # Maggie