我的Django / Mezzanine项目中有这个课程:
class SitePermission(models.Model):
"""
Permission relationship between a user and a site that's
used instead of ``User.is_staff``, for admin and inline-editing
access.
"""
user = models.OneToOneField(user_model_name, verbose_name=_("Author"),
related_name="%(class)ss")
sites = models.ManyToManyField("sites.Site", blank=True,
verbose_name=_("Sites"))
class Meta:
verbose_name = _("Site permission")
verbose_name_plural = _("Site permissions")
在管理员中并选择其中一个站点并按下保存按钮。记录使用权限进入数据库。
如何在前端执行以下操作:
这是我目前处理此问题的观点:
def create_site(request):
if request.method == "POST":
form = CreateSite(request.POST)
if form.is_valid():
try:
post = form.save(commit=False)
post.save()
return HttpResponseRedirect('/welcome/')
except IntegrityError:
form._errors["domain"] = ["Domain must be unique!"]
else:
form = CreateSite()
return render(request, "includes/create_site.html", {'form': form})
forms.py:
class CreateSite(ModelForm):
class Meta:
model = Site
fields = ['domain' , 'name']