gpt4 book ai didi

python - Django2.1 : Queryset ModelChoice in a ModelForm then save two forms

转载 作者:行者123 更新时间:2023-12-01 00:35:12 25 4
gpt4 key购买 nike

我希望当前登录的用户能够创建并保存这两个表单。第二种形式具有ManyToManyField 和ForeignKey 关系,如下所示。

目前,我尝试从 ChatGroupUser ModelForm 查询当前用户。

使用当前代码,当我尝试访问该页面时,Django 会引发以下错误:

__init__() missing 1 required positional argument: 'user'

它说错误来自第 88 行,即:

form_user = ChatGroupUserForm() 

完整回溯

Traceback (most recent call last):
File "/Users/macadmin/Documents/Django_fun4/Groupixx/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/macadmin/Documents/Django_fun4/Groupixx/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/macadmin/Documents/Django_fun4/Groupixx/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/macadmin/Documents/Django_fun4/Groupixx/groupixx/core/views.py", line 88, in create_group
form_user = ChatGroupUserForm()
TypeError: __init__() missing 1 required positional argument: 'user'

模型.py

class ChatGroup(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
group_admin = models.ForeignKey(User, on_delete=models.CASCADE, related_name='chat_group_admins')
is_active = models.BooleanField(default=True, null=True)
is_current = models.BooleanField(default=False, null=True)

class ChatGroupUser(models.Model):
user = models.ManyToManyField(User)
chat_group = models.ForeignKey(ChatGroup, on_delete=models.CASCADE, related_name='chat_group_users')

forms.py

from django.contrib.auth import get_user_model

User = get_user_model()

class ChatGroupForm(forms.ModelForm):
class Meta:
model = ChatGroup
fields = ['name',]

class ChatGroupUserForm(forms.ModelForm):
class Meta:
model = ChatGroupUser
fields = ['user']

def __init__(self, user, *args, **kwargs):
super(ChatGroupUserForm, self).__init__(*args, **kwargs)
self.fields['user'].queryset = User.objects.filter(user=user)

views.py

def create_group(request):
if request.method == 'POST':
form_group = ChatGroupForm(request.POST)
form_user = ChatGroupUserForm(request.POST)

if form_group.is_valid():
create_form = form_group.save(commit=False)
create_form.group_admin = request.user
create_form.save()
form_user.save(commit=False)
form_user.user = user
form_user.save()
return redirect('core:group_list')

else:
form_group = ChatGroupForm()
form_user = ChatGroupUserForm()

context = {
'form_group':form_group,
'form_user': form_user
}
return render(request, 'core/create-group.html', context)

我将不胜感激任何帮助,我不确定为什么 django 会抛出错误以及为什么我无法创建和保存这两种表单。

最佳答案

这是因为你的init函数有参数user
而您的 View 未通过用户

修复方法如下
在您看来:
form_user = ChatGroupUserForm(user=request.user)

然后在您的表单中:

class ChatGroupUserForm(forms.ModelForm):
class Meta:
model = ChatGroupUser
fields = ['user']

def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(ChatGroupUserForm, self).__init__(*args, **kwargs)
self.fields['user'].queryset = User.objects.filter(user=self.user)

希望它有效:)

关于python - Django2.1 : Queryset ModelChoice in a ModelForm then save two forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57847409/

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