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

  • SEARCH
  • Home
  • 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 976149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:42:04+00:00 2026-05-16T03:42:04+00:00

I have a django model as following class Project(models.Model) name=models.CharField(max_length=200) class Application(models.Model) proj=models.ForeignKey(Project, null=True,

  • 0

I have a django model as following

class Project(models.Model)
      name=models.CharField(max_length=200)

class Application(models.Model)
      proj=models.ForeignKey(Project, null=True, blank=True)

I need to modify the admin form of the project to be able to assign multiple applications to the project, so in the admin.py I have created a ModelAdmin class for the project as following

class ProjectAdmin(ModelAdmin)
      form=projectForm
      project_apps=[]

and the project form as following

class ProjectForm(forms.ModelForm):
    class Meta:
       model = Project

    project_apps =forms.ModelMultipleChoiceField(queryset=Application.objects.all(),required=False,)
def __init__(self, *args, **kwargs):
    super(ProjectForm, self).__init__(*args, **kwargs)
    if self.instance.id is not None:
        selected_items = [ values[0] for values in Application.objects.filter(project=self.instance) ]
        self.fields['project_apps'].initial = selected_items

def save(self,commit=True):
    super(ProjectForm,self).save(commit)
    return self.instance

by doing this I have a multiple select in the create/edit project form.
what I need is to override the save method to save a reference for the project in the selected applications?

how can I get the selected applications ????

  • 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-16T03:42:05+00:00Added an answer on May 16, 2026 at 3:42 am

    Not entirely sure what you’re trying to do, but maybe this?

    def save(self,commit=True):
        kwargs.pop('commit') # We're overriding this with commit = False
        super(ProjectForm,self).save(commit)
        if self.instance:
            for a in self.cleaned_data['project_apps']:
                a.proj = self.instance
                a.save()
        return self.instance
    

    Now, I can’t remember if in this case, self.cleaned_data['project_apps'] will actually contain a list of Application objects or not. I suspect it will, but if not this function will take care of that:

    def clean_project_apps(self):
        app_list = self.cleaned_data['project_apps']
        result = []
        for a in app_list:
            try:
                result.append(Application.objects.get(pk=a)
            except Application.DoesNotExist:
                raise forms.ValidationError("Invalid application record") # to be safe
        return result
    

    All in all I think this form is a bad idea though, because basically what is happening here is you’re displaying all of the application records which doesn’t make sense, since most of them will be associated with other projects.

    Oh oh oh!!! Just noticed you wanted this to show up in a Multiple Select list!

    You’re (probably) doing it wrong

    A multiple select means this isn’t a one-to-many relationship. It’s a many-to-many relationship.

    This is what you want to do, easy peasy, doesn’t require any custom forms or anything.

    class Project(models.Model)
          name=models.CharField(max_length=200)
          project_apps = models.ManyToMany('Application', null=True, blank=True)
    
    class Application(models.Model)
          # nothing here (NO foreign key, you want more than one App/Proj and vice versa)
    

    Indicating that this is a many-to-many field in Project will automagically create the multiple select box in admin. Ta da!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.