Learning Django.. I am trying to setup the below and need your expert advise to make it better..
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:
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:
Queries:
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
Since sub-category and products are not unique relation to its parent, use the ForeignKey
Field to connect them.
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.
Yet I cannot see the usecase of your Userlist
model.
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. Then on form submition, you Query for the correct Category
and Sub-Category
instance and assign it to the ForeignKey
field of your form.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.