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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:11:02+00:00 2026-05-28T08:11:02+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)

And I want to create a form for Film. The form should have a drop down for Studio, but allow the user to enter a new Studio if the one they need isn’t already in the database. First, if there’s a simpler way to implement this, let me know.

But what I’m trying to do is include a “+” icon next to the Studio dropdown. If this is clicked, it unhides a new field that allows the user to enter text for a new Studio. I want to include a “-” button next to the unhid field that rehides it if the user changes their mind. That’s where I’m getting stuck.

My form currently looks like this:

class SelectWithPlus(forms.Select):
    def render(self, name, *args, **kwargs):
        html = super(SelectWithPlus, self).render(name, *args, **kwargs)
        plus = render_to_string("form/plus.html", {'field': name})
        return html+plus

class DeSelectWithX(forms.CharField):
    def render(self, name, *args, **kwargs):
        html = super(DeSelectWithX, self).render(name, *args, **kwargs)
        ex = render_to_string("form/ex.html", {'field': name})
        return html+ex

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

    def __init__(self, *args, **kwargs):
        super(MdlForm, self).__init__(*args,**kwargs)
        self.fields['new_studio'].widget.attrs['class'] = 'hidden_studio_field'

The idea is that the studio field has a widget that adds a bit of html for a “+” icon:

<a
    href="JavaScript:void()"
    class="add-another"
    id="add_id_{{ field }}">
    <img src="http://mydbupload.s3.amazonaws.com/icon_addlink.gif" width="10" height="10" Border ="0" alt="Add New {{ field }}"/>
</a>

When this is clicked, it triggers jquery to show the fields named appropriately:

<script type="text/javascript">
    $(document).ready(function() {
        $(".hidden_studio_field").hide();
        $('label[for="id_new_studio"]').hide();
    });

    $(function () {
        $('#add_id_studio').click(function() {
            $(".hidden_studio_field").show();
            $('label[for="id_new_studio"]').show();
        });
    });
</script>

So I want to add a similar “-” button to the new_studio field. I tried to replicate the SelectWithPlus widget, but the one snag I’m hitting is that I can no longer use the bit of code that adds the class “hidden_studio_field” to that field. I get an error: ‘DeSelectWithX’ object has no attribute ‘attrs’

So short question: How do I add a css-type class to a field that is defined by a widget?

  • 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-28T08:11:03+00:00Added an answer on May 28, 2026 at 8:11 am

    DeSelectWithX should inherit from forms.TextInput, not forms.CharField.

    • 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.