Last active
July 11, 2022 06:37
-
-
Save benjaoming/8ae29830034a051795c7a2cf58b87cb8 to your computer and use it in GitHub Desktop.
Django non-db field properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Ever got tired of having loads of migrations that have no effect on | |
the actual database? | |
This little mockup presents how we can move properties of fields into | |
a scope where they can easily be ignored by the migration system | |
Would it work? | |
""" | |
from django.db import models | |
from django.utils.translation import gettext_lazy as _ | |
class Inquiry(models.Model): | |
email = models.EmailField(null=False) | |
sent_by = models.CharField(max_length=256, null=True) | |
product = models.ForeignKey('shop.Product', on_delete=models.CASCADE) | |
attachment = models.FileField(null=True) | |
class Meta: | |
unique_together = ("email", "product") | |
order = ("product") | |
class FieldMeta: | |
""" | |
Everything here is ignored by migrations | |
""" | |
verbose_names = { | |
'email': _("email"), | |
'sent_by': _("person name"), | |
'product': _("product"), | |
'attachment': _("photo or pdf drawing"), | |
} | |
help_texts = { | |
'person_name': _("may be left blank"), | |
'attachment': _( | |
"we can find you the right shoe if you send a " | |
"picture or drawing of your foot" | |
), | |
} | |
blank = { | |
'person_name': True, | |
} | |
extra = { | |
'attachment': {'upload_to': '/inquiries/'}, | |
} | |
class CompanyInquiry(Inquiry): | |
items = models.IntegerField(default=0) | |
class FieldMeta: | |
verbose_names = { | |
'sent_by': _("company or person"), # Override parent verbose name! | |
'items': _("number of items") | |
} | |
choices = { | |
'items': [(1, "1"), (2, "2"), (10, "10")], | |
} | |
help_text = { | |
'items': _("We only deliver in these quantities, which is relevant?"), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment