简体   繁体   English

Django下拉列表取决于模型?

[英]Django drop down list dependant on model?

I'm trying to make a drop down list dependent on a model.我正在尝试制作一个依赖于模型的下拉列表。 For example my app has an organization model, when a user is created an organization is set for them.例如,我的应用程序有一个组织模型,当创建用户时,会为他们设置一个组织。 Organizations can have stores.组织可以有商店。 When a logged in user accesses this drop down list, I want to list all the stores that are associated with the organization that the user is set to.当登录用户访问此下拉列表时,我想列出与用户设置的组织相关联的所有商店。 What is the best way to do this?做这个的最好方式是什么?

models.py模型.py


class Org(models.Model):

  name = models.CharField(blank=True, max_length=100)
  client_id = models.IntegerField()
  default_store = models.OneToOneField('Store',
    on_delete=models.SET_NULL,
    null=True,blank=True,
    related_name='+')

  def __str__(self):
    return self.name

class Store(models.Model):

  name = models.CharField(blank=True, max_length=100)
  org = models.ForeignKey('Org', on_delete=models.CASCADE)
  store_id = models.IntegerField(null=True, blank=True)

  def __str__(self):
    return self.name


class Profile(models.Model):
  user = models.OneToOneField(User, on_delete=models.CASCADE)
  org = models.ForeignKey('Org', 
    on_delete=models.SET_NULL,
    null=True, blank=True)

  def __str__(self):
    return self.user.username

@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
  if created:
    Profile.objects.create(user=instance)
  instance.profile.save()

I would like to use this drop down list as part of the bootstrap nav bar where I have other drop down lists (associated).我想将此下拉列表用作引导程序导航栏的一部分,其中我有其他下拉列表(关联)。 Here is the section of the nav bar code where I would like to use it.这是我想使用它的导航条码部分。 I know it won't fit exactly as I have below but this is an example of how I am making a different drop down list我知道它不完全符合我下面的要求,但这是我如何制作不同下拉列表的示例

            <div class="btn-group">
              <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Reports
              </button>
              <div class="dropdown-menu">
                <a class="dropdown-item" href="{% url 'something' %}">Store_a1</a>
                <a class="dropdown-item" href="{% url 'something' %}">Store_a2</a>
                <a class="dropdown-item" href="{% url 'something' %}">Store_a3</a>

              </div>
            </div>

The page loads an iframe url where the store_id is passed in as a query parameter currently the store_id of the "default_store" is being passed.该页面加载一个 iframe url,其中 store_id 作为查询参数传入,当前正在传递“default_store”的 store_id。 I'm thinking I need a variable for something like "selected_store" or something alone those lines?我在想我需要一个变量来表示“selected_store”之类的东西还是那些行? When the user clicks one of the stores on the drop down I would like the store_id of the selected store to be passed to the query param in the iframe url and refresh the page.当用户单击下拉列表中的商店之一时,我希望将所选商店的 store_id 传递给 iframe url 中的查询参数并刷新页面。

Thanks谢谢

Based on your models you should be able to do something like this in your template to loop over all stores for a user根据您的模型,您应该能够在模板中执行类似操作以遍历用户的所有商店

{% for store in request.user.profile.org.store_set.all %}
    <a class="dropdown-item" href="{% url 'something' store_id=store.id %}">{{ store }}</a>
{% endfor %}

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

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