Vincent John Labrador
Vincent John Labrador

Reputation: 63

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

I'm stuck with a problem to filter project managers from a foreign key field in django forms. I want to exclude superusers and other user roles except project manager.

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' 

Upvotes: 1

Views: 231

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476699

You check the condition with:

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

you can also explicitly exclude is_inventory_admin=True and is_client=True (in case a CustomUser can be two or three at the same time), with:

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

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.

Upvotes: 2

Related Questions