Ad
In Django How To Add User To Many-to-many Filed
I like to add permission to those users who I add to a many-to-many relation.
I have a Projekt
model where I like to add multiple users like this:
class Projekt(models.Model):
def __str__(self):
return str(self.projekt)
projekt = models.TextField(max_length=150)
company_name = models.ForeignKey('Company', on_delete=models.CASCADE, default=1)
jogosult_01 = models.ManyToManyField(User)
date = models.DateField(auto_now_add=True, auto_now=False, blank=True)
I add the users in the Django Admin page with holding down cmd then save and it shows everything is OK.
If try to get the values like this:
{% for u in jogosult %}
{{ u.jogosult_01 }}
{% endfor %}
it say auth.user none
.
views.py
@login_required
def projects(request):
jogosult = Projekt.objects.filter(jogosult_01_id=request.user).order_by('-date')
context = {
'jogosult': jogosult,
}
return render(request, 'stressz/projects.html', context)
Ad
Answer
User
objects have access to their related Projekt
objects:
@login_required
def projects(request):
jogosult = request.user.projekt_set.all() #<-- HERE!
context = {
'jogosult': jogosult,
}
return render(request, 'stressz/projects.html', context)
Is full documented at Many-to-many relationships
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad