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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:01:19+00:00 2026-05-30T18:01:19+00:00

I having a weird issue with Django 1.3. When I try to add a

  • 0

I having a weird issue with Django 1.3. When I try to add a new Activity using the ActivityForm, the Activity is often updated even if I explicitly create a new Activity object with not Id.

In addition, when the init of my ActivityForm class has an explicit trip_id parameter, the UI displays “Select a valid choice. That choice is not one of the available choices.” for the field location (even though the selection shown is valid). However, if I make this an optional parameter and I pop it from kwargs, I don’t see this issue.

Can someone please take a look at the code below and see what I am doing wrong?

forms.py

class DestinationMultipleChoiceField(ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return obj.city_name

class ActivityForm(forms.Form):
    description = forms.CharField(max_length=100, required=True)
    location = DestinationChoiceField(queryset = Visit.objects.none(), empty_label=None, required=True)

    def __init__(self, trip_id = None, *args, **kwargs):
        super(ActivityForm, self).__init__(*args, **kwargs)
        if trip_id is not None:
            self.fields['location'].queryset = Visit.objects.filter(trip=trip_id).all().select_related('destination')

    # A new Activity() is created if nothing is provided
    def save(self, activity = Activity()):
        if not self.is_valid():
            raise forms.ValidationError('ActivityForm was not validated first before trying to call save().')

        activity.visit = self.cleaned_data['location']
        activity.description = self.cleaned_data['description']
        activity.added_by = User.objects.get(pk=1)
        activity.save()

views.py

def add_activity(request, trip_id = None, template_name = 'edit-form.html'):
    if request.POST:
        form = ActivityForm(trip_id, request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('views.list_trip'))
    else:
        form = ActivityForm(trip_id)

    return render_to_response(template_name, {
        'page_title': 'Add',
        'form': form,
    }, context_instance=RequestContext(request))
  • 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-30T18:01:21+00:00Added an answer on May 30, 2026 at 6:01 pm

    Look at this line:

    def save(self, activity = Activity()):
    

    In Python, default parameters are evaluated once; thus, when the save method is called the first time, a new Activity will be created, but subsequent calls will use that Activity.

    Change save to something like this:

    def save(self, activity=None):
        if activity is None:
            activity = Activity()
        # Rest of the method
    

    Then, a new Activity will be created on every call (if one is not supplied as an argument).

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

Sidebar

Related Questions

I'm having a bit of a weird issue here, basically, im using hibernate together
Having a weird issue. I'm new to Macs and have a windows VM that
I am having this weird issue with Cruise Control crashing, when I add the
I am having a weird issue in ASP.NET MVC with objects not being updated
Hello good morning everyone, i am having a weird issue when using imagePickerController. Actually
I'm using Heroku for the first time and am having a weird issue. I've
Im having a weird compile issue using the Google App engine in java using
I'm having a weird issue in a program I'm developing for .NET 3.5 using
Having a weird issue with jQuery/JavaScript. I'm using this code: var minimumPercent = $('.donationTextBox').val();
This is a weird issue I'm having, I have a table and try to

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.