筛选多对多字段表单'str'对象没有属性'get'



我在 models.py

中有这些类别
class Parent(models.Model):
    title = models.CharField()
class Child(models.Model): 
    title = models.CharField()
    parent = models.ForeignKey(Parent)
class Address(models.Model)
    title = models.CharField()
    parent = models.ForeignKey(Parent)
    child = models.ManyToManyField(Child)

因为我想要"地址"模型中的"儿童"字段仅显示与"父"相关的"儿童"对象,所以我将此代码写入我的 forms.py

class AddressForm(forms.ModelForm):
    class Meta:
        model = Address
        fields = ('title', 'parent', 'child')
    def __init__(self, parent_id, *args, **kwargs):
        super(AddressForm, self).__init__(*args, **kwargs)
        self.fields['child'].queryset = Child.objects.filter(parent__id=parent_id)

views.py

def address(request, parent_id):
    parent = get_object_or_404(Parent, id=parent_id)
    if request.method == 'POST':
        form = AddressForm(request.POST, parent_id)
        if form.is_valid():
            address = form.save(commit=False)
            address.parent = parent
            address.save()
            return redirect('app:address_added')
    else:
        form = AddressForm(parent_id)
    template = "add_address.html"
    context = {'form': form}
    return render(request, template, context)

结果:儿童字段仅显示与所请求的父对象有关的子对象。我想要什么。

问题:当我提交时,我会收到此错误:

attributeError at/manage/add_address/'str'对象没有属性 '获取'

追溯:

文件 "/library/frameworks/python.framework/3.5/lib/python3.5/site-packages/django/core/core/handlers/exception.py" 在内部 39.响应= get_response(请求)

文件 "/library/frameworks/python.framework/3.5/lib/python3.5/site-packages/django/core/core/bandlers/base.py.py" 在_get_response中 187.响应= self.process_exception_by_middleware(E,请求)

文件 "/library/frameworks/python.framework/3.5/lib/python3.5/site-packages/django/core/core/bandlers/base.py.py" 在_get_response中 185.响应= wrapped_callback(请求, *callback_args,** callback_kwargs)

在 add_address 167.如果form.is_valid():

文件 "/library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/django/forms/forms.py" 在IS_VALID中 169.返回self.is_bound而不是self.errors

文件 "/library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/django/forms/forms.py" 错误 161. self.full_clean()

文件 "/library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/django/forms/forms.py" 在full_clean中 370. self._clean_fields()

文件 "/library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/django/forms/forms.py" 在_clean_fields中 382. value = field.widget.value_from_datadict(self.data,self.files, self.add_prefix(name))

文件 "/library/frameworks/python.framework/3.5/lib/python3.5/site-packages/django/django/forms/widgets.py" 在value_from_datadict中 427. upload = super(clearableFileInput,self).value_from_datadict(数据,文件,名称)

文件 "/library/frameworks/python.framework/3.5/lib/python3.5/site-packages/django/django/forms/widgets.py" 在value_from_datadict中 354.返回files.get(name)

异常类型:attributeError at/manage/add_address/exception 值:'str'对象没有属性'get'

请帮助?

您已将parent_id成为表格的第一个位置参数,因此您应该在邮局中这样通过:

form = AddressForm(parent_id, request.POST)

请注意,最好不要完全更改表单的签名,而是使用Kwargs:

def __init__(self, *args, **kwargs):
    parent_id = kwargs.pop('parent_id', None)
    super(AddressForm, self).__init__(*args, **kwargs)

和:

form = AddressForm(request.POST, parent_id=parent_id)

在邮局和

form = AddressForm(parent_id=parent_id)

在其他中。

相关内容

  • 没有找到相关文章

最新更新