python - 在 Django 的 subview 中获取 url 父参数

标签 python django

我正在尝试配置我的项目以供多个企业使用。我的意思是一个用户可以是一个用户的经理,另一个用户的客户。用户可以这样选择想要的企业: enter image description here

所以我需要根据使用的企业来放置url,例如:

在 kiriosnet 企业:

  • www.kinet.com/kiriosNet/index
  • www.kinet.com/kiriosNet/new_order/。 .

在 checkcell 企业

  • www.kinet.com/checkcell/index
  • www.kinet.com/checkcell/new_order/
  • www.kinet.com/checkcell/employees/

等等...

我使用中间模型来使用 django groups 选择角色:

class UsuarioEmpresa(models.Model):
    usuario = models.ForeignKey('Usuario')
    empresa = models.ForeignKey(Empresa)
    rol = models.ForeignKey(Group)
    active = models.BooleanField(default=True)

    class Meta:
        verbose_name = "UsuarioEmpresa"
        verbose_name_plural = "UsuariosEmpresas"

    def __str__(self):
        return "%s de %s" %(self.usuario, self.empresa)

我在我的全局网址中尝试这样:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    # Enterprise admin
    url(r'^(?P<enterprise_name>[\w]+)/', include([
        url(r'^', include('app.urls')),
        url(r'^reparaciones/', include('reparaciones.urls', namespace='reparaciones')),
        url(r'^usuarios/', include('usuarios.urls', namespace='usuarios')),
        url(r'^equipos/', include('equipos.urls', namespace='equipos')),
        url(r'^empresas/', include('empresas.urls', namespace='empresas')),
        url(r'^reportes/', include('reportes.urls', namespace='reportes')),
    ]))

]

应用程序网址:

urlpatterns = [
    # The home page
    url(r'^$', views.index, name='index'),
    ...
]

我的应用索引上的 views.py 是:

# Index user loged in
@login_required
def index(request, enterprise_name):
    enterprise = Empresa.objects.filter(name__icontains = 'enterprise_name')

我在尝试http://127.0.0.1:8000/kiriosNet/

时遇到以下错误
NoReverseMatch at /kiriosNet/
Reverse for 'index' with arguments '()' and keyword arguments '{}' not 
found. 1 pattern(s) tried: ['(?P<enterprise_name>[\\w]+)/$']

在我的中间模型中使用“事件” bool 字段来选择实际企业有多明智?我想现在就这么做。

最佳答案

您尝试做的事情称为 Multi-Tenancy ,并且有一个非常好的库,称为 django-tenant-schema .

它的工作原理是将不同客户端的数据以不同模式存储在同一数据库中。它需要一个模型来负责管理此模式切换:

from django.db import models
from tenant_schemas.models import TenantMixin

class Client(TenantMixin):
    name = models.CharField(max_length=100)
    paid_until =  models.DateField()
    on_trial = models.BooleanField()
    created_on = models.DateField(auto_now_add=True)

    # default true, schema will be automatically created and synced when it is saved
    auto_create_schema = True

我已经尝试过了,非常顺利。

关于python - 在 Django 的 subview 中获取 url 父参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54636342/

相关文章:

python - 来自 2D pandas 数据框的 Matplotlib 3D 曲面图

python - 在拆分应用程序服务器/数据库服务器环境中安装 python 的 mariadb-devel 和 python34-mysql-debug 包的位置

python - Pandas 时间子集时间序列 - 特定时间以上的日期

python - 通过排除字段使用更新 View Django 编辑模型对象

python - 我想解决 Django NoReverseMatch

python - 获取行中条件匹配的列名

python - 为什么我的大文件在 Google Colab 上提取后尺寸非常小?

python - 在 Django 管理对象页面中显示对象 ID(主键)

python - 在 django admin 中过滤下拉值

Django Queryset - 通过 model.choices 获取计数