Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以django表单访问易趣等产品子类别_Django_Forms - Fatal编程技术网

以django表单访问易趣等产品子类别

以django表单访问易趣等产品子类别,django,forms,Django,Forms,我试图复制易趣等网站在发布产品时使用的类别结构。例如,如果您想发布一部Iphone进行销售,您可以在发布广告时,从表单(“电子和计算机”)的下拉菜单中选择一个类别,然后选择“电话”的子类别,然后选择“Iphone”的最后一个子类别。为了创建这个结构,我使用django类别。在admin page works文件中创建产品时,admin表单允许我从每个类别的下拉菜单中进行选择,但是我似乎无法在自己的表单上复制相同的过程,以便用户能够从众多类别中进行选择 如果您不知道django是否分类,那么这是一

我试图复制易趣等网站在发布产品时使用的类别结构。例如,如果您想发布一部Iphone进行销售,您可以在发布广告时,从表单(“电子和计算机”)的下拉菜单中选择一个类别,然后选择“电话”的子类别,然后选择“Iphone”的最后一个子类别。为了创建这个结构,我使用django类别。在admin page works文件中创建产品时,admin表单允许我从每个类别的下拉菜单中进行选择,但是我似乎无法在自己的表单上复制相同的过程,以便用户能够从众多类别中进行选择

如果您不知道django是否分类,那么这是一个修改的前序树遍历

这是我的广告模型

class Advert(models.Model):

    title = models.CharField(max_length=100, blank=False, null=False)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)
    image = models.ImageField(upload_to='media/', blank=True, null=True)  
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
    quantity = models.IntegerField(blank=False, null=False)
    condition = models.CharField(max_length=10, choices=COND_CATEGORIES, blank=False, null=False)
    price = models.DecimalField(decimal_places=2, max_digits=14, blank=False, null=False)
    description = models.TextField(blank=False, null=False)
    date_posted = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    active = models.BooleanField(default=True)
    featured = models.BooleanField(default=False)
    search_vector = SearchVectorField(null=True, blank=True)

这是分类模型

class Category(CategoryBase):


    class Meta:
        verbose_name_plural = 'categories'
在这里,我提供了一个允许用户发布广告的表单

class PostAdvertForm(forms.ModelForm):

    title = forms.CharField(label='Ad Title', required=True)
    category = forms.ChoiceField(choices=Advert.category, label='Choose a category', required=True)
    price = forms.DecimalField(label='Price', required=True, widget=forms.TextInput())                                  
    description = forms.CharField(widget=forms.Textarea(attrs={'placeholder':
                                          ('Please provide a detailed description'),
                                          'autofocus': 'autofocus'}), label='Description', required=True)
    condition = forms.ChoiceField(choices=Advert.COND_CATEGORIES, label='Condition', required=True)
    quantity = forms.IntegerField(label='Quantity', required=True, widget=forms.TextInput())
    image = forms.ImageField(label='Upload an image', required=False)
    class Meta:
        model = Advert
        fields = (
            'title', 'category', 'quantity', 'condition', 'price', 'description', 
            'image')
由于“ForwardManyToneDescriptor的对象不可编辑”,在选择字段上使用advert.category无效

我的问题是如何让类别列表显示在choicefield上

编辑: 我只是想知道,这是否可以完全通过前端的jquery实现,并通过已清理的数据发送choosen类别?这样,我甚至不需要在后端使用django类别,我只需要存储大量类别

第二次编辑:

看起来我已经完成了第一部分的工作,我已经将新的表单代码粘贴到下面:

at = Advert.category.get_queryset()


class PostAdvertForm(forms.ModelForm):

    title = forms.CharField(label='Ad Title', required=True)
    category = forms.ModelChoiceField(queryset=cat, label='Choose a category', required=True)
    price = forms.DecimalField(label='Price', required=True, widget=forms.TextInput())                                  
    description = forms.CharField(widget=forms.Textarea(attrs={'placeholder':
                                          ('Please provide a detailed description'),
                                          'autofocus': 'autofocus'}), label='Description', required=True)
    condition = forms.ChoiceField(choices=Advert.COND_CATEGORIES, label='Condition', required=True)
    quantity = forms.IntegerField(label='Quantity', required=True, widget=forms.TextInput())
    image = forms.ImageField(label='Upload an image', required=False)
    class Meta:
        model = Advert
        fields = (
            'title', 'category', 'quantity', 'condition', 'price', 'description', 
            'image')

我以前从未使用过jquery,这是创建流动下拉式菜单的最佳选项吗?我必须这样做,这样他们就不能选择父类别,只能选择最下面的子类别。

定义一个函数
更新\u表单\u字段\u选项
(例如在
utils.py
中):

然后,在
PostAdvertForm
中定义方法
\uuuuu init\uuuu

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
在这个函数中,调用这个函数,并用一个返回正确选项的函数替换
your_choices()
。记住选择格式(元组的元组)


还请记住,
ModelForm
为您定义了表单字段,因此您不必显式地定义它们,除非您想更改默认定义中的某些内容。

您可以在
PostAdvertForm
def\uu init\uuuuuu
中给出选项。看一看
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    update_form_field_choices(field=self.fields['category'], choices=your_choices())