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 7195005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:29:27+00:00 2026-05-28T20:29:27+00:00

I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio

  • 0

I have two models:

class Studio(models.Model):
    name = models.CharField("Studio", max_length=30, unique=True)

class Film(models.Model):
    studio = models.ForeignKey(Studio, verbose_name="Studio")
    name = models.CharField("Film Name", max_length=30, unique=True)

I have a Film form that allows the user to either select a preexisting Studio, or type in a new one:

class FilmForm(forms.Form):
    studio = forms.ModelChoiceField(Studio.objects, required=False)
    new_studio = forms.CharField(max_length=30, required=False, label = "New Studio Name")
    name = forms.CharField(max_length=30, label = "Film Name")

There’s validation to assure that the new_studio name doesn’t already exist. If the user enters a new_studio, I want to save the studio and then save the new Film.

form = FilmForm(request.POST) 
if form.is_valid(): # All validation rules pass
    std = Studio(name = form.cleaned_data['new_studio'])
    std.save()

But then how do I save the Film instance subject to the brand new studio id? I’ve seen this question, but what if I have many more fields in the Film model and Film Form? If I use the linked answer, I would have to enter each field:

studio = Studio.objects.get(name=request.POST['new_studio'])
newFilm=Film(name=form.name, studio=studio, field_one = form.field_one, field_two = form.field_two, etc.)

What is the correct way to implement this?

  • 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-28T20:29:29+00:00Added an answer on May 28, 2026 at 8:29 pm

    Really, your only problem is that you’ve used a standard Form instead of a ModelForm. Form doesn’t have a save method because it’s not inherently tied to anything (i.e. it doesn’t know what to save or where to save to).

    However, if you use a ModelForm you need to take care of all logic involved in creating a new studio in the form. This is actually better, though, because then you can just use the form and not worry about anything else: the form holds all the logic needed to properly save itself.

    class FilmForm(forms.ModelForm):
        class Meta:
            model = Film
    
        # only need to define `new_studio`, other fields come automatically from model
        new_studio = forms.CharField(max_length=30, required=False, label = "New Studio Name")
    
        def __init__(self, *args, **kwargs):
            super(FilmForm, self).__init__(*args, **kwargs)
            # make `studio` not required, we'll check for one of `studio` or `new_studio` in the `clean` method
            self.fields['studio'].required = False
    
        def clean(self):
            studio = self.cleaned_data.get('studio')
            new_studio = self.cleaned_data.get('new_studio')
            if not studio and not new_studio:
                # neither was specified so raise an error to user
                raise forms.ValidationError('Must specify either Studio or New Studio!')
            elif not studio:
                # get/create `Studio` from `new_studio` and use it for `studio` field
                studio, created = Studio.objects.get_or_create(name=new_studio)
                self.cleaned_data['studio'] = studio
    
            return super(FilmForm, self).clean()
    

    Then, in your view, all you need is:

    if form.is_valid():
        form.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
I have these two models : class Module(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(unique=True,
I have following two models class Questionnaire(models.model) name = models.CharField(max_length=128, null=True, blank=True) type =
In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
I have two models with slug fields: class Book(models.Model): name = models.CharField(max_length=200) slug =
if I have two simple models: class Tag(models.Model): name = models.CharField(max_length=100) class Post(models.Model): title
If I have two models like class Author(models.Model): name = models.CharField(max_length=100) title = models.CharField(max_length=3,
I have two models class Status(models.Model) name = models.CharField(max_length=25) rank = models.PositiveSmallIntegerField() class MyModel(models.Model)
I have two models class Employer(models.Model): name = models.CharField(max_length=300, blank=False) id = models.IntegerField() status

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.