Saturday, June 30, 2012

Re: invalid keyword argument for this function

Hi Nikhil,

You can not use an object's M2M field until it has been saved to the db.
So, you would have to call the "save" method on the instance, or use "create" method to create the instance.
            category_obj = Category.objects.create(
                            type = categoryform.cleaned_data['type']
            event_public = categoryform.cleaned_data['event_public']
            event_genre = categoryform.cleaned_data['event_genre']
            # No need to call save here

The reason behind this is that M2M Fields are represented by intermediate tables with references to the two linked objects. If you need to add a row in it, both referenced objects should already be saved in the db with valid primary keys.


On Saturday, June 30, 2012 1:21:30 PM UTC+5:30, Nikhil Verma wrote:
Hi All

I have the following models like this :-

class EventGenre(models.Model):
    genre_choices = models.CharField(max_length=255)
    def __unicode__(self):
        return self.genre_choices
class EventPublic(models.Model):
    choicelist = models.CharField(max_length=255)
    def __unicode__(self):
        return self.choicelist

class Category(models.Model):
    Describes the Event Category
    type = models.CharField(max_length=20,\
                                  help_text = "type of event"
    # Genre of event like classical,rock,pop ,indie etc
    event_genre = models.ManyToManyField(EventGenre)
    # audience for this event like adults ,children etc
    event_public = models.ManyToManyField(EventPublic)
    def __unicode__(self):
        return self.type

This is my category form

class CategoryForm(forms.Form):
    type = forms.CharField(max_length=80, \
    event_genre = forms.ModelMultipleChoiceField(
                        queryset = EventGenre.objects.all(),
    event_public = forms.ModelMultipleChoiceField(
                        queryset = EventPublic.objects.all(),                         

This is my

def eventcreation(request):
    if request.method == "POST":
        event_form = EventForm(request.POST,request.FILES,prefix="eventform")
        categoryform = CategoryForm(request.POST)
        if event_form.is_valid():
            event_obj = Event(
            venue_name = event_form.cleaned_data['venue_name'],
            date_created = event_form.cleaned_data['event_start_date'],
            date_completed = event_form.cleaned_data['event_end_date'],
            event_ticket_price = event_form.cleaned_data['event_ticket_price'],
            eventwebsite = event_form.cleaned_data['eventwebsite'],
            keyword = event_form.cleaned_data['keyword'],
            description = event_form.cleaned_data['description'],
            status = event_form.cleaned_data['status'],
            event_poster = event_form.cleaned_data['event_poster']
        if categoryform.is_valid():
            category_obj = Category(
                            type = categoryform.cleaned_data['type'],
                            event_public = categoryform.cleaned_data['event_public'], # It is giving error
                            event_genre = categoryform.cleaned_data['event_genre'],   # It is giving error   
            print "Form is getting Invalid"
        event_form = EventForm()
        categoryform = CategoryForm()
    return render_to_response('event/event.html',
I am trying to add a ManyToMany Field and gets this traceback :-

Exception Type: TypeError at /event/createevent/
Exception Value: 'event_public' is an invalid keyword argument for this function

Can anybody point out whats going wrong ?

Thanks in advance.

Nikhil Verma

You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment