python - 如何根据登录的用户限制多对多字段中的选择?
问题描述
我希望该视图仅显示当前用户创建的“人物”。
如果我没有理解错误,我要做的是在表单呈现之前过滤选择,具体取决于当前登录的用户是谁。我想我必须编辑的是queryset
参数,ModelChoiceField(**kwargs)
但我不知道我必须在哪里做这个。
模型.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Autor(models.Model):
nombre = models.CharField(max_length=40)
apellido = models.CharField(max_length=40)
usuario = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
def __str__(self):
return "%s %s" % (self.nombre, self.apellido)
class Personaje(models.Model):
nombre = models.CharField(max_length=40)
personalidad = models.CharField(max_length=20)
creador = models.ForeignKey(Autor, on_delete=models.CASCADE)
def __str__(self):
return "nombre: %s Creador: %s" % (self.nombre, self.creador)
class Historia(models.Model):
autor = models.ForeignKey(Autor, on_delete=models.CASCADE)
personajes = models.ManyToManyField(Personaje)
desarrollo = models.TextField()
视图.py
简单地说,我还没有捕捉到 request.user.id (我真的被过滤器所困扰)我知道该怎么做,硬编码用户 id 就可以了。
from django.shortcuts import render
from django.views.generic import CreateView
from core.models import Historia
# Create your views here.
class CrearHistoria(CreateView):
model = Historia
fields = ['personajes', 'desarrollo']
template_name = 'core/crear_historia.html'
当前结果:
我想要完成的事情:
解决方案
I have been struggling with this days but here is my solution.
from django.shortcuts import render
from django.views.generic import CreateView
from core.models import Historia, Personaje
# Create your views here.
class CrearHistoria(CreateView):
model = Historia
fields = ['personajes', 'desarrollo']
template_name = 'core/crear_historia.html'
def get_form(self, form_class=None):
"""Return an instance of the form to be used in this view."""
if form_class is None:
form_class = self.get_form_class()
my_form_class = form_class(**self.get_form_kwargs())
my_form_class['personaje'].field.queryset = Personaje.objects.filter(autor__id=2)
return my_form_class
How I came to this solution: How do I filter ForeignKey choices in a Django ModelForm? Here I found that it's needed define the queryset attribute. And here (This web is awesome) I found that I can access the form instantiated by the CreateView.
But the hardest part was find the correct syntax to define the queryset so after search a lot I decided do it by my self:
def get_form(self, form_class=None):
"""Return an instance of the form to be used in this view."""
if form_class is None:
form_class = self.get_form_class()
my_form_class = form_class(**self.get_form_kwargs())
print("Las variables son: ", vars(my_form_class))
print("Las variables son:", vars(my_form_class['personaje'].field))
print("El valor del atributo es:", my_form_class['personaje'].field.queryset)
my_form_class['personaje'].field.empty_label = '(No hay selecciones)'
my_form_class['personaje'].field.queryset = Personaje.objects.filter(autor__id=2)
return my_form_class
In case that you need filter by user you will pass the self.request.user.id
value to the filter method.
Here is the prints outputs:
Las variables son: {'instance': , '_validate_unique': False, 'is_bound': False, 'data': {}, 'files': {}, 'auto_id': 'id_%s', 'initial': {}, 'error_class': , 'label_suffix': ':', 'empty_permitted': False, '_errors': None, 'fields': OrderedDict([('titulo', ), ('autor', ), ('personaje', ), ('desarrollo', )]), '_bound_fields_cache': {}, 'renderer': } Las variables son: {'empty_label': '---------', 'required': True, 'label': 'Personaje', 'initial': None, 'show_hidden_initial': False, 'help_text': '', 'disabled': False, 'label_suffix': None, 'localize': False, 'widget': , 'error_messages': {'required': 'Este campo es obligatorio.', 'invalid_choice': 'Seleccione una opción válida. La opción seleccionada no es una de las disponibles.'}, 'validators': [], '_queryset': , ]>, 'limit_choices_to': {}, 'to_field_name': 'id'} El valor del atributo es: , ]>
I hope that this could help anyone who needs a little modification in a EditView. Finally thanks for the previous answer but that's not was what I was looking for.
推荐阅读
- java - 试图验证负数
- xcode - Xcode 12 中的类型检查错误:“编译器无法在合理的时间内对该表达式进行类型检查”
- laravel - 测试 E2E Cypress Laravel Jetstream 失败点击
- java - 如何在 JAVA 中将此以下 CURL 更改为 REST 客户端?
- javascript - 未捕获的错误:Reference.set 失败:第一个参数在属性“Users.ji8A7TkSldfdvieDqGxko9eV3Jy1.gender”中包含未定义
- sql - 计算另一列中某个值出现次数的存储过程
- python - 多个字段包括用 python 和 mysql 排除
- arrays - VBA 数组函数的工作速度比 vlookup 慢
- tarantool - 如何向现有索引添加新字段?
- list - 我想在颤振中为购物应用程序制作类别页面?