J
J
JonGalt2017-05-29 18:03:29
Django
JonGalt, 2017-05-29 18:03:29

Your django middleware?

models

class Project(models.Model):
    short_name = models.CharField(verbose_name=u'краткое название', max_length=16)
    full_name = models.TextField(verbose_name=u'полное название', blank=True)

class ProjectUser(models.Model):
    project = models.ForeignKey(Project, null=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    user_role = models.ForeignKey(UserRole)
    company = models.ForeignKey(Company, verbose_name=u'Организация')
    default_project = models.BooleanField(verbose_name=u'Проект по умолчанию')

how to make a request.project that would pass the parameters of the current project
project = Project.objects.get(projectuser__user=request.user, projectuser__default_project=1)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Scherbakov, 2017-05-29
@JonGalt

class ProjectMiddleware:
    def process_request(self, request):
        assert hasattr(request, 'user'), (
            "The Project middleware "
            "requires authentication middleware to be installed. Edit your "
            "MIDDLEWARE_CLASSES setting to insert "
            "'django.contrib.auth.middleware.AuthenticationMiddleware'. "
            "If that doesn't work, ensure your TEMPLATE_CONTEXT_PROCESSORS "
            "setting includes 'django.core.context_processors.auth'."
        )

        if request.user.is_authenticated():
            request.project = ...

Don't forget to add the class path to settings.MIDDLEWARE_CLASSES.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question