invalid keyword argument for this function

480 views
Skip to first unread message

Nikhil Verma

unread,
Jun 30, 2012, 3:51:30 AM6/30/12
to django...@googlegroups.com
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,\
                                  choices=EVENT_TYPE,\
                                  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, \
                           widget=RadioSelect(choices=EVENT_TYPE)
                           )
    event_genre = forms.ModelMultipleChoiceField(
                        queryset = EventGenre.objects.all(),
                        widget=CheckboxSelectMultiple,
                        required=False
                        )
    event_public = forms.ModelMultipleChoiceField(
                        queryset = EventPublic.objects.all(),                         
                        widget=CheckboxSelectMultiple,
                        required=False
                        )

This is my views.py

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   
                            )
           
            category_obj.event_genre.add(event_genre),
            category_obj.event_public.add(event_public),
            category_obj.save()
           
           
        else:
            print "Form is getting Invalid"
    else:
      
        event_form = EventForm()
        categoryform = CategoryForm()
    return render_to_response('event/event.html',
                              {
                              'eventform':event_form,
                              'categoryform':categoryform,
                              },
                              context_instance=RequestContext(request)
                              )
           
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.


--
Regards
Nikhil Verma
+91-958-273-3156

Sunny Nanda

unread,
Jun 30, 2012, 9:38:50 AM6/30/12
to django...@googlegroups.com
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']
            category_obj.event_genre.add(event_genre),
            category_obj.event_public.add(event_public),
            # No need to call save here
            # category_obj.save() 

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.

H2H
-Sandeep

Nikhil Verma

unread,
Jun 30, 2012, 10:44:28 AM6/30/12
to django...@googlegroups.com
Hi Sunny

I realized that earlier sunny, and cleared that defect but i get stuck into this error

Traceback:


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

Any help ?

Thanks in advance


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/9y6fVygzELMJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Sunny Nanda

unread,
Jun 30, 2012, 11:00:08 AM6/30/12
to django...@googlegroups.com
Hi Nikhil,

Just to reiterate :), you need to remove both event_public & even_genre while creating the instance of the model, since both of them are M2M Fields. Take a look at the example in my previous mail.

If you are getting this exception somewhere else, please send that code snippet so we might able to see the problem.

Thanks,
Sandeep


On Saturday, June 30, 2012 8:14:28 PM UTC+5:30, Nikhil Verma wrote:
Hi Sunny

I realized that earlier sunny, and cleared that defect but i get stuck into this error

Traceback:

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

Any help ?

Thanks in advance


To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Nikhil Verma

unread,
Jun 30, 2012, 11:06:36 AM6/30/12
to django...@googlegroups.com
Hi Sunny

I am trying with this

            event_genre = event_form.cleaned_data['event_genre']
            event_public = event_form.cleaned_data['event_public']
            event_obj.save()

            category_obj.event_genre.add(event_genre),
      category_obj.event_public.add(event_public



and getting this traceback:-


Exception Type: TypeError at /event/createevent/
Exception Value: int() argument must be a string or a number, not 'QuerySet'

To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/zrcm159aC3YJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Nikhil Verma

unread,
Jun 30, 2012, 11:07:34 AM6/30/12
to django...@googlegroups.com
Yes its with the braces ")" also.

Sunny Nanda

unread,
Jun 30, 2012, 11:26:40 AM6/30/12
to django...@googlegroups.com
This is a different error, and I don't see anything in the code snippet that might have caused this error (except if you are doing something in the Event model's save method)

You would need to debug it further, and pinpoint the location of this error.
Regards
Nikhil Verma
+91-958-273-3156

Nikhil Verma

unread,
Jun 30, 2012, 12:23:15 PM6/30/12
to django...@googlegroups.com
Yes its different.I was trying to say i tried your way and get rid of that error.

Thanks

To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Ft4a4VmU_sgJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply all
Reply to author
Forward
0 new messages