如何在外键 django 表单中过滤用户作为选择?

How can I filter users as a choice in a foreign key django forms?

我在从 Django 表单的外键字段中过滤项目经理时遇到了问题。我想排除除项目经理之外的超级用户和其他用户角色。

Models.py

class Projects(models.Model):

id=models.AutoField(primary_key=True)
project_name=models.CharField(max_length=255)
project_manager=models.ForeignKey(CustomUser,on_delete=models.CASCADE, limit_choices_to={'is_project_manager__eq : 1 '})
client_name=models.ForeignKey(Clients,on_delete=models.CASCADE, null=True)
project_pic=models.ImageField(upload_to='static/website/project-images')
project_start_date=models.DateField(null=True)
project_end_date=models.DateField(null=True)
project_description=models.TextField(null=True)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
is_draft = models.BooleanField(default=True)
objects=models.Manager()

class Meta:
    verbose_name_plural = 'Project'

def __str__(self):
    return f'{self.project_name}'

Forms.py

class ProjectForm(forms.ModelForm): 
class Meta: 
    model = Projects
    fields = ['project_name', 'project_manager', 'client_name', 'project_pic', 'project_start_date', 'project_end_date', 'project_description']

Views.py

def project_list(request):

projects = Projects.objects.all()

if request.method == 'POST':
    form = ProjectForm(request.POST, request.FILES)
    if form.is_valid():
        form.save()
        project_name = form.cleaned_data.get('project_name')
        messages.success(request, f'{project_name} has been successfully added.')
        return redirect('project_list')
else:
    form = ProjectForm()

context = {

    'projects' : projects,
    'form' : form,

}

template_name ='project-admin/project-list.html'
return render(request, template_name, context)

CustomUser/Models.py

Here is their own roles to set them as a project manager, inventory admin, and client

class CustomUser(AbstractUser):

is_inventory_admin = models.BooleanField('Is Inventory Admin', default=False)
is_project_manager = models.BooleanField('Is Project Manager', default=False)
is_client = models.BooleanField('Is Client', default=False)

class Meta:
    db_table = 'auth_user' 

您检查条件:

class Projects(models.Model):
    # …
    project_manager = models.ForeignKey(
        CustomUser,
        on_delete=models.CASCADE,
        <strong>limit_choices_to={'is_project_manager': True}</strong>
    )
    # …

你也可以明确排除is_inventory_admin=Trueis_client=True(如果CustomUser可以同时是两个或三个),用:

class Projects(models.Model):
    # …
    project_manager = models.ForeignKey(
        CustomUser,
        on_delete=models.CASCADE,
        <strong>limit_choices_to={'is_project_manager': True, 'is_inventory_admin': False, 'is_client': False}</strong>
    )
    # …

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.


Note: normally a Django model is given a singular name, so Project instead of Projects.