0

I'm new here. I just stared a project using Python/Django it is an idea that I want to show at my job.

I have a django project and app already working Since I just started with it. I'm relying heavily on the default Django Admin Panel.

My Problem is: I have this class in models.py

class Sales(models.Model):
    order_number = models.CharField(max_length=50)
    order_date = models.DateField('digitado em', default=date.today)
    Sales_User = models.ForeignKey(User, on_delete=models.SET_DEFAULT)

Now in my User db I have 50 Users. But only 15 of those are Sales People (all in the Group "Sales")

When I go to create an order and have to select the sales user The dropdown shows all 50 Users. Is the a way to either filter those user by the group they are in? Or to replace the dropdown with on of those pop-up then search and select.

I'm open to try different approaches as long as I can actually accomplish them. :-)

Thank you all for the time.


Edit:

After hours since i posted it here, I finally got it.

First thing i did since then (don't think i had a effect but its a variable from the original problem) was to extend the Django user system with

AUTH_USER_MODEL

again. Not sure if it had any influence.

Then

I tried creating another table in the user extended model. U know. So I could add another field into User Form

The Table had only

user_type = models.CharField(max_length=30)

I went to the admin panel and added a few like Sales Workers Partners

Then I went to a user (any user) settings e chose a field (in this case Sales)

After all that. I still didn't do anything to actually filter the primary key.

But after looking in the django files (more precisely the one where Primary keys were written and I found the option limit_choices_to={'' : ''}

I tried and that was it. Exactly like i wanted. ;-)

So now. it looks like this

class Sales(models.Model):
    order_number = models.CharField(max_length=50)
    order_date = models.DateField('digitado em', default=date.today)
    Sales_User = models.ForeignKey(settings.AUTH_USER_MODEL, limit_choices_to={'types_of_users' : '1'}, on_delete=models.SET_DEFAULT)

In this example "1" the key for the option in the table I set to the User.

if you still want to filter by groups (one) instead of creating a table just replace 'types_of_users' with 'groups'

Hope any one who needs can understand how I explained it.

Happy Coding.

1 Answers1

0

See this thread: filter foreignkey field in django admin and instead of name filter by Sales_User__group (or whatever the User's group field is)

Chris
  • 500
  • 6
  • 25