In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
I have learned how to add, delete and change a single table in Django. Let's learn how to create a foreign key constraint.
Example 1
Models.py
From django.db import modelsclass UserGroup (models.Model): uid = models.AutoField (primary_key=True) caption = models.CharField (max_length=32,unique=True) ctime = models.DateTimeField (auto_now_add=True, null=True) uptime = models.DateTimeField (auto_now=True, null=True) class UserInfo (models.Model): username = models.CharField (max_length=32,blank=True,verbose_name=' user name') password = models.CharField (max_length=60 Help_text='pwd') email = models.CharField (max_length=60) test = models.EmailField (max_length=19,null=True,error_messages= {'invalid':' Please enter password'}) # user_group_id numeric user_group = models.ForeignKey ("UserGroup", to_field='uid') # (uid,catption,ctime,uptimew) user_type_choices = ((1, 'superuser'), (2 'ordinary user'), (3, 'ordinary user'), user_type_id = models.IntegerField (choices=user_type_choices,default=1)
The key statement to create a foreign key constraint is a command.
User_group = models.ForeignKey ("UserGroup", to_field='uid') # (uid,catption,ctime,uptime)
Note a few points:
To_field points to the primary key of another class
By default, he creates a cascading delete constraint, that is, if I delete a row of data in user_group, the rows associated with this uid data in the userInfo table will be deleted automatically; we can disable deletion through on_delete=models.SET_NULL
In UserInfo's table, he automatically creates a new field, user_group_id, as a foreign key field, instead of the user_group defined in our class. But interestingly, we can refer to the user_group object directly in the userInfo object in the program.
For the tables created in the database, pay attention to the field names
Let's look at an example of a call
Example 2. You can see that I can get the information of the user group table directly through row.user_group.caption, which is equivalent to performing a query operation of left join.
Views.py
Def user_info (request): if request.method = = "GET": user_list = models.UserInfo.objects.all () group_list = models.UserGroup.objects.all () return render (request, 'user_info.html', {' user_list': user_list, "group_list": group_list})
User_info.html
Add user {% for item in group_list%} {{item.caption}} {% endfor%} user list {% for row in user_list%} {{row.username}} | {{row.user_group.caption}} delete | Edit {% endfor%}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.