11# Permafrost Forms
22from django .conf import settings
33from django .contrib .auth .models import Permission
4+ from django .contrib .sites .models import Site
45from django .core .exceptions import ValidationError
56from django .forms import ModelForm
67from django .forms .fields import CharField , ChoiceField , BooleanField
@@ -66,7 +67,7 @@ class Meta:
6667
6768 def __init__ (self , * args , ** kwargs ):
6869 super ().__init__ (* args , ** kwargs )
69-
70+ self . site = kwargs . pop ( 'site' , Site . objects . get_current ())
7071 self .fields ['category' ].choices = CHOICES
7172
7273 category = self .initial .get (
@@ -86,6 +87,7 @@ def __init__(self, *args, **kwargs):
8687 bootstrappify (self .fields )
8788
8889 def save (self , commit = True ):
90+ self .instance .site = self .site
8991 instance = super ().save (commit )
9092 category = instance .category
9193
@@ -109,7 +111,7 @@ def clean_name(self):
109111 name_exists = PermafrostRole .objects .filter (
110112
111113 name = name ,
112- site__id = settings . SITE_ID ,
114+ site = self . site ,
113115
114116 ).exclude (pk = self .instance .pk ).first ()
115117
@@ -118,7 +120,7 @@ def clean_name(self):
118120 try :
119121 name_exists = PermafrostRole .objects .get (
120122 name = name ,
121- site__id = settings . SITE_ID
123+ site = self . site
122124 )
123125 except PermafrostRole .DoesNotExist :
124126 pass
0 commit comments