gpt4 book ai didi

python - 悬垂于多对多关系的内联表单集

转载 作者:太空狗 更新时间:2023-10-30 01:15:21 25 4
gpt4 key购买 nike

按照 Kevin Dias 在 this article 中的说明进行操作,我尝试为两个相关模型生成一个表格。这似乎适用于一对多关系,但我在使用多对多关系时遇到了问题。

下面是一些用户角色管理的示例代码:

#models.py
from django.db import models

class Role(models.Model): # for each role there can be multiple users
role_name=models.CharField(max_length=20)

class User(models.Model): # each user can have multiple roles
name=models.CharField(max_length=20)
role=models.ManyToManyField(Role, through='UserRole')

class UserRole(models.Model): # table to store which user has which roles
role=models.ForeignKey(Role)
user=models.ForeignKey(User)

# forms.py
from django.forms import ModelForm
from django.forms.models import inlineformset_factory

from rightmanagement.models import Role, User

class UserForm(ModelForm):
class Meta:
model = User

RoleFormSet = inlineformset_factory(User, Role) # this is probably the line that causes the problem

# views.py
from django.http import HttpResponseRedirect
from rightmanagement.models import User
from rightmanagement.forms import RoleFormSet, UserForm

# Create view
from django.views.generic import CreateView
class UserCreate(CreateView):
model = User
form_class = UserForm

def get(self, request, *args, **kwargs):
"""
Handles GET requests and instantiates blank versions of the form
and its inline formsets.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
role_form = RoleFormSet()
return self.render_to_response(
self.get_context_data(form=form,
role_form=role_form))

def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance and its inline
formsets with the passed POST variables and then checking them for
validity.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
role_form = RoleFormSet(self.request.POST)
if (form.is_valid() and role_form.is_valid()):
return self.form_valid(form, role_form)
else:
return self.form_invalid(form, role_form)

def form_valid(self, form, role_form):
"""
Called if all forms are valid. Creates a Recipe instance along with
associated Ingredients and Instructions and then redirects to a
success page.
"""
self.object = form.save()
role_form.instance = self.object
role_form.save()
return HttpResponseRedirect(self.get_success_url())

def form_invalid(self, form, role_form):
"""
Called if a form is invalid. Re-renders the context data with the
data-filled forms and errors.
"""
return self.render_to_response(
self.get_context_data(form=form,
role_form=role_form))

这些设置导致错误消息 <class 'rightmanagement.models.Role'> has no ForeignKey to <class 'rightmanagement.models.User'> .

做一些研究我发现这个:Django inlineformset_factory and ManyToMany fields .似乎内联表单集仅适用于 ForeignKey但不适用于 ManyToManyField .还有 the docs可以这样解读。

但是,我认为在这种特殊情况下,外键而不是多对多关系没有任何意义。对于多对多关系,Django 内置内联表单集的挂件看起来如何?目标是构建一个表单,允许将用户分配给已经存在的角色,或者创建新角色并将它们分配给用户(如果它们尚不存在)。

最佳答案

您可能已经知道,您无法使用内联表单集编辑多对多关系。但是,您可以编辑 through 模型。所以对于你的内联表单集,你只需要将模型设置为直通模型,就像这样:

RoleFormSet = inlineformset_factory(UserRole, User.role.through)

关于python - 悬垂于多对多关系的内联表单集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22238690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com