Skip to content

Commit ea7eb7a

Browse files
committed
Fix Django schema deprecation
1 parent d374d88 commit ea7eb7a

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

devel/forms.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.contrib.auth.models import Group, User
88
from django.contrib.sites.models import Site
99
from django.core.mail import send_mail
10+
from django.db.models import URLField
1011
from django.forms.widgets import NumberInput
1112
from django.template import loader
1213

@@ -32,6 +33,16 @@ def clean(self):
3233
return self.cleaned_data
3334

3435

36+
# Assume all URLField will be HTTPS if not specified.
37+
# NOTE: this can be removed once we bump Django to 6.x
38+
# where `https` becomes the default. Since we are using
39+
# form.ModelForm, we do this to apply https to all UrlField.
40+
def _urlfields_assume_https(db_field, **kwargs):
41+
if isinstance(db_field, URLField):
42+
kwargs["assume_scheme"] = "http"
43+
return db_field.formfield(**kwargs)
44+
45+
3546
class UserProfileForm(forms.ModelForm):
3647
def clean_pgp_key(self):
3748
data = self.cleaned_data['pgp_key']
@@ -46,6 +57,7 @@ class Meta:
4657
widgets = {
4758
'yob': NumberInput(attrs={'min': 1950, 'max': date.today().year - 10}),
4859
}
60+
formfield_callback = _urlfields_assume_https
4961

5062

5163
class NewUserForm(forms.ModelForm):
@@ -58,6 +70,7 @@ class NewUserForm(forms.ModelForm):
5870
class Meta:
5971
model = UserProfile
6072
exclude = ('picture', 'user')
73+
formfield_callback = _urlfields_assume_https
6174

6275
def __init__(self, *args, **kwargs):
6376
super(NewUserForm, self).__init__(*args, **kwargs)

0 commit comments

Comments
 (0)