Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7653897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:08:28+00:00 2026-05-31T12:08:28+00:00

An EU can use this form to select the roles (Project Manager, Developer etc)

  • 0

An EU can use this form to select the roles (Project Manager, Developer etc) they play for any given project. I want to limit the project field options to only those in the employees department. Right now, the EU can select any departments’ project (but not the department, I’ve excluded that entirely) How can I do that? queryset=blabla doesnt work..

MODELS:

class Department(models.Model):
    name = models.CharField(max_length=20)
    def __unicode__(self):
        return self.name

class Employee(models.Model):
    fname = models.CharField(max_length=15)
    department = models.ForeignKey(Department)
    def __unicode__(self):
        return self.fname

class Projecttype(models.Model):
    name = models.CharField(max_length=20)
    def __unicode__(self):
        return self.name

class Project(models.Model):
    projecttype = models.ForeignKey(Projecttype)
    department = models.ForeignKey(Department)
    members = models.ManyToManyField(Employee, through='Membership')
    def __unicode__(self):
       return "%s > %s" % (self.department, self.projecttype)

class Role(models.Model):
    name = models.CharField(max_length=20)
    def __unicode__(self):
       return self.name

class Membership(models.Model):
    project = models.ForeignKey(Project, null=True)
    department = models.ForeignKey(Department)
    employee = models.ForeignKey(Employee)
    role = models.ManyToManyField(Role, blank=True, null=True)
    class Meta:
        unique_together = (("project", "employee",),)

VIEW:

def employee_edit(request, employee_id):
    i = get_object_or_404(Employee, pk=employee_id)
    EmployeeInlineFormSet = inlineformset_factory(Employee, Membership, extra=1, exclude=('department',), queryset=Membership.objects.filter(department=i.department))
    if request.method == "POST":
        f = EmployeeInlineFormSet(request.POST, instance=i)
        if f.is_valid():
            f.save()
    else:
        f = EmployeeInlineFormSet(instance=i)
    return render_to_response('testdb/edit.html', {'item': i, 'formset': f, }, context_instance=RequestContext(request))

JSON: MANAGE.PY DUMPDATA TESTDB –INDENT=4

[
    {
        "pk": 1,
        "model": "testdb.department",
        "fields": {
            "name": "IT Department"
        }
    },
    {
         "pk": 2,
        "model": "testdb.department",
        "fields": {
            "name": "Operations Department"
        }
    },
    {
        "pk": 1,
        "model": "testdb.employee",
        "fields": {
            “department”: 1,
            "fname": "Alice"
        }
    },
    {
        "pk": 2,
        "model": "testdb.employee",
        "fields": {
            “department”: 2,
            "fname": "Eve"
        }
    },
    {
        "pk": 3,
        "model": "testdb.employee",
        "fields": {
            “department”: 1,
            "fname": "Bob"
        }
    },
    {
        "pk": 1,
        "model": "testdb.projecttype",
        "fields": {
            "name": "PROCESS IMPROVEMENT"
        }
    },
    {
        "pk": 2,
        "model": "testdb.projecttype",
        "fields": {
            "name": "DATA CLEANUP"
        }
    },
    {
        "pk": 1,
        "model": "testdb.project",
        "fields": {
            “projecttype”: 1,
            “department”: 1
        }
    },
    {
        "pk": 2,
        "model": "testdb.project",
        "fields": {
            “projecttype”: 1,
            “department”: 2
        }
    },
    {
        "pk": 3,
        "model": "testdb.project",
        "fields": {
            “projecttype”: 2,
            “department”: 1
        }
    },
    {
        "pk": 1,
        "model": "testdb.role",
        "fields": {
            "name": "Project Manager"
        }
    },
    {
        "pk": 2,
        "model": "testdb.role",
        "fields": {
            "name": "Analyst"
        }
    },
    {
        "pk": 1,
        "model": "testdb.membership",
        "fields": {
            "employee": 1,
            “department”: 1,
            “project”: 1,
            "role": [
                1,
                2
            ]
        }
    },
    {
        "pk": 2,
        "model": "testdb.membership",
        "fields": {
            "employee": 2,
            “department”: 2,
            “project”: 2,
            "role": [
                1
            ]
        }
    },
    {
        "pk": 3,
        "model": "testdb.membership",
        "fields": {
            "employee": 3,
            “department”: 1,
            “project”: 1,
            "role": [
                1
            ]
        }
    }
]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:08:29+00:00Added an answer on May 31, 2026 at 12:08 pm

    Don’t pass the queryset when you’re creating the formset class here:

    EmployeeInlineFormSet = inlineformset_factory(Employee, Membership, extra=1, exclude=('department',))
    

    Pass the queryset when instantiating the formset:

    f = EmployeeInlineFormSet(instance=i, queryset=Membership.objects.filter(department=i.department))
    

    e.g.:

    def employee_edit(request, employee_id):
        i = get_object_or_404(Employee, pk=employee_id)
        queryset=Membership.objects.filter(department=i.department)
        EmployeeInlineFormSet = inlineformset_factory(Employee, Membership, extra=1, exclude=('department',))
        if request.method == "POST":
            f = EmployeeInlineFormSet(request.POST, instance=i, queryset=queryset)
            if f.is_valid():
                f.save()
        else:
            f = EmployeeInlineFormSet(instance=i, queryset=queryset)
        return render_to_response('testdb/edit.html', {'item': i, 'formset': f, }, context_instance=RequestContext(request))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically want to autodetect a user's timezone using Rails. I can use this
I want to use php array for HTML select list. In this case it
Right now I can use this URL to request a Google Static Maps image
So in my documentation it says: public event TreeViewPlusNodeCheckedEventHandler NodeChecked() You can use this
This is a silly question, but you can use this code to check if
In SQL Server 2017, you can use this syntax, but not in earlier versions:
In a 32-bit .NET app, I can use this OLEDB connection string to connect
Simple. If I use: public void Add(params int[] values) Then I can use this
To grab the inode of a file in PHP, you can use this: $fs
Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand(GetSomething, new SqlConnection(Config.ConnectionString)) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.