简体   繁体   中英

Combine Data of Two Tables in single DropDown using Django

Problem : I have Two Tables Users and Users Group and in front end on request of that particular page need to send all data from both the table together, as there is specific drop down to show them both, and after done with the operation of that page data will get back in POST request (current models structures is given below), i am not getting how do i make connection in all these three tables so that it will get managed, Please let me know.

Model: User.py

class Users(AbstractBaseUser):
    vendor_name = models.ForeignKey(Vendor, on_delete=models.CASCADE, default=None, null=True)
    username = models.CharField(max_length=100, verbose_name="username", unique=True)
    password = models.CharField(max_length=100)
    created_by = models.DateField(verbose_name="created_by", auto_now_add=True)

    USERNAME_FIELD = "username"
    REQUIRED_FIELDS = ['password', 'hardware_id']

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_role_vendor = models.BooleanField(default=False)
    is_role_customer = models.BooleanField(default=True)

    def __str__(self):
        return self.username

    objects = UserManager()

Model: UserGroup.py

class UserGroup(models.Model):
    vendor_id = models.ForeignKey(Vendor, on_delete=models.CASCADE, default=None, null=True)
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=1000)
    users = models.ManyToManyField(Users)

    def __str__(self):
        return self.name

Model: Rules.py

class Rules(models.Model):
    vendor_id = models.ForeignKey(Vendor, on_delete=models.CASCADE, default=None, null=True)
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=1000)
    # Here i need to mention the code for field where i can store the combined value of User and UserGroup [selected from the dropdown].

    def __str__(self):
        return self.name

Need Solution:

  1. How do i code in View to fetch the data of Two tables to send them for DropDown. {GET Request}
  2. How will i store the values for the same together in Rules Table { As i said DropDown consist both values and can be selected all together. }
  3. The Structure of the Model {with the required Changes}.

there is no out of the box solution for that. I can advise to seprate this dropdown into two. First with UserGroup, second with User. You can fill user dropdown based on selected UserGroup with ajax or htmx -> htmx value-select

In your model Rules (should be Rule) add fields:

 user = models.ForeignKey(Users, on_delete=models.CASCADE)
 group = models.ForeignKey(UserGroup, on_delete=models.CASCADE)

if there can be only one rule per Users(this should also be User)/UserGroup add unique_toigether to model Rules:

unique_together = ['user', 'group']

django docs unique_together

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.

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