Django--models Many-to-many

Source: Internet
Author: User


Application Scenarios

When you create a row of data in a table, there is a drop-down box with multiple choices (one-to-single box)
For example, to create user information, you need to assign multiple hobbies to the user

Create a table

Either way, one is to use Django to automatically create the third table associated with many-to-many, and the other is to manually create the relational table yourself.


Automatically create a third table correspondence for many-to-many tables, based on the code in Django

12345678 class Host(models.Model):    hostname = models.CharField(max_length=32)    port = models.IntegerField()class HostAdmin(models.Model):    username = models.CharField(max_length=32)    email = models.CharField(max_length=32)    host = models.ManyToManyField(Host)

Custom Many-to-many tables, no ORM are generated by themselves
123456789101112 class Host1(models.Model):    hostname = models.CharField(max_length=32)    port = models.IntegerField()class HostAdmin1(models.Model):    username = models.CharField(max_length=32)    email = models.CharField(max_length=32)    host = models.ManyToManyField(Host1, through=‘HostRelation‘)class HostRelation(models.Model):    c1 = models.ForeignKey(Host1)    c2 = models.ForeignKey(HostAdmin1)
123456789 #多对多自定义创建表    models.HostRelation.objects.create(        c1=models.Host1.objects.get(id=1),        c2=models.HostAdmin1.objects.get(id=2)    )    models.HostRelation.objects.create(        c1_id=2,        c2_id=1    )

Create data
1234567 初始化数据    models.Host.objects.create(hostname=‘c1‘,port=80)    models.Host.objects.create(hostname=‘c2‘,port=80)    models.Host.objects.create(hostname=‘c3‘,port=80)    models.HostAdmin.objects.create(username=‘root‘,email=‘[email protected]‘)    models.HostAdmin.objects.create(username=‘dali‘,email=‘[email protected]‘)    models.HostAdmin.objects.create(username=‘haojie‘,email=‘[email protected]‘)

Add data
123456789101112 #正向添加    #目的,给大力分配两个主机的管理权限    #1、获取大力用户    admin_obj = models.HostAdmin.objects.get(username=‘dali‘)    #2、获取指定的两个主机    host_list = models.Host.objects.filter(id__lt=3)    #3、将两个主机和大力添加到第三张对应表中*host_list)  #反向添加    host_obj = models.Host.objects.get(id=3)    admin_list = models.HostAdmin.objects.filter(id__gt=1)    host_obj.hostadmin_set.add(*admin_list)

Query data?
123456789101112131415 #查询数据  #第一种方式    #正向查    admin_obj = models.HostAdmin.objects.all(id=1)    for item in admin_obj:    #反向查    host_obj = models.Host.objects.get(id=1)    host_obj.hostadmin_set.all()  #第二种方式    relation_list = models.HostRelation.objects.all()    relation_list = models.HostRelation.objects.filter(c2__username=‘dali‘)    for item in relation_list:        print item.c1.hostname        print item.c2.username

From for notes (Wiz)

Django--models Many-to-many

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.