Hi, I’m working on a PR for pythondotorg. While my solution works, I’m not sure if I’m following best practices. I couldn’t find many resources on this topic, so I would appreciate any advice.
Original model:
class Membership(models.Model):
BASIC = 0
SUPPORTING = 1
SPONSOR = 2
MANAGING = 3
CONTRIBUTING = 4
FELLOW = 5
MEMBERSHIP_CHOICES = (
(BASIC, 'Basic Member'),
(SUPPORTING, 'Supporting Member'),
(SPONSOR, 'Sponsor Member'),
(MANAGING, 'Managing Member'),
(CONTRIBUTING, 'Contributing Member'),
(FELLOW, 'Fellow'),
)
membership_type = models.IntegerField(default=BASIC, choices=MEMBERSHIP_CHOICES)
...
New model:
class Membership(models.Model):
BASIC = 0
SUPPORTING = 1
CONTRIBUTING = 2
FELLOW = 3
MEMBERSHIP_CHOICES = (
(BASIC, 'Basic Member'),
(SUPPORTING, 'Supporting Member'),
(CONTRIBUTING, 'Contributing Member'),
(FELLOW, 'Fellow'),
)
membership_type = models.IntegerField(default=BASIC, choices=MEMBERSHIP_CHOICES)
...
Trying to automate the process of updating records with a membership level scheduled for deletion:
# Generated by Django 4.2.17 on 2025-01-12 04:08
from django.db import migrations, models
def update_membership_levels(apps, schema_editor):
SPONSOR = 2
MANAGING = 3
CONTRIBUTING = 4
Membership = apps.get_model('users', 'Membership')
Membership.objects.filter(
membership_type__in=[SPONSOR, MANAGING]
).update(membership_type=CONTRIBUTING)
class Migration(migrations.Migration):
dependencies = [
('users', '0015_alter_user_first_name'),
]
operations = [
migrations.RunPython(update_membership_levels),
migrations.AlterField(
model_name='membership',
name='membership_type',
field=models.IntegerField(choices=[(0, 'Basic Member'), (1, 'Supporting Member'), (2, 'Contributing Member'), (3, 'Fellow')], default=0),
),
]
My understanding is that this approach uses the legacy model; therefore, membership_level
4 is still available. Any records that match SPONSOR or MANAGING are updated to CONTRIBUTING. Then, when AlterField
is applied, CONTRIBUTING (4) is changed to CONTRIBUTING (2). Is this correct?
After a quick search on GitHub, I wasn’t able to find any instances of this approach being used, so I wonder if it’s uncommon or considered a non-standard practice. Any advice or clarification on the underlying behavior would be greatly appreciated.