简体   繁体   English

显示来自 Model Django 的唯一值的表格

[英]Form to show unique values from Model Django

Learning Django.. I am trying to setup the below and need your expert advise to make it better..学习 Django .. 我正在尝试设置以下内容,需要您的专家建议以使其更好..

I created a model, and form on top of it to show the data.. and I have two questions after the code where I need your advise:)我创建了一个 model,并在其上形成以显示数据.. 在代码之后我有两个问题需要您的建议:)

Model: Model:

class profilestable(models.Model):
   choices = [
   ('Active','Active'),
   ('Inactive','Inactive')
   ]
   Category = models.CharField(max_length=100)
   SubCategory = models.CharField(max_length=100)
   product_name = models.CharField(max_length=100)
   Status = models.ChoiceField(max_length=20,choices=choices)

   def __str__(self):
      return self.Category

class Userlist(models.Model):
       User = models.CharField(max_length=100)
       Categorygroup = models.ForeignKey(profilestable,null=true,on_delete=models.SET_NULL)
    
       def __str__(self):
          return self.User

Form:形式:

class userprofileform(forms.ModelForm):
   model = Userlist
   Fields = ('User','CategoryGroup')

def __init__(self,*args,**kwargs):
   super(userprofileform,self).__init__(*args,**kwargs)

Sample data in the profilestable Model:配置文件表 Model 中的示例数据:

在此处输入图像描述

Queries:查询:

  1. I want to add the category, subcategory and product name as drop downs to the form however I am not sure how to access each element to show on the form.我想将类别、子类别和产品名称作为下拉列表添加到表单中,但是我不确定如何访问要在表单上显示的每个元素。 I was able to pull only category since I am returning that value.因为我要返回该值,所以我只能提取类别。
  2. The list currently has lots of duplicate values, is it possible to show only unique values in the drop down.该列表当前有很多重复值,是否可以在下拉列表中仅显示唯一值。
  3. Also, is it possible to make it a multi-select and dependent cascading drop downs此外,是否可以使其成为多选和依赖级联下拉菜单

Request you to please help advise/direct to implement this type of form.请您帮助建议/指导实施此类表格。

Thank you so much.太感谢了。

The structure needs some changes, for example category should be a parent of sub-category , and then product lives below sub-category , so it would be better to create a separates models for Category , Sub-category and Product in this relation:结构需要进行一些更改,例如category应该是sub-category的父级,然后product位于sub-category之下,因此最好在此关系中为CategorySub-categoryProduct创建一个单独的模型:

Category > Sub-category > Product Since sub-category and products are not unique relation to its parent, use the ForeignKey Field to connect them. Category > Sub-category > Product由于子类别和产品与其父类别不是唯一的关系,因此使用ForeignKey字段将它们连接起来。

Also I see in your sample data and many products are shinked in 1 row, that is not good in any terms of scaling, performance or order.此外,我在您的sample data中看到,许多产品都缩小为 1 行,这在缩放、性能或订单方面都不好。

Yet I cannot see the usecase of your Userlist model.但是我看不到您的用户列表Userlist的用例。

Once done the above please refer this answer to create your drop down menu, althought the distinct at the end is not needed as creating separate model for Category will avoid the need to duplicate Category names.完成上述操作后,请参考此答案来创建您的下拉菜单,尽管最后不需要区分,因为为Category创建单独的 model 将避免重复Category名称的需要。 Then on form submition, you Query for the correct Category and Sub-Category instance and assign it to the ForeignKey field of your form.然后在提交表单时,查询正确的CategorySub-Category实例并将其分配给表单的ForeignKey字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM