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:
-- 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 advanceOn Sat, Jun 30, 2012 at 7:08 PM, Sunny Nanda wrote:
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['eve
nt_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
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,\
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
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-users@googlegroups.com.
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 .
--
Regards
Nikhil Verma
+91-958-273-3156
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/-/zrcm159aC3YJ.
To post to this group, send email to django-users@googlegroups.com.
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.
No comments:
Post a Comment