Django save_m2m() and excluded field

Posted by jul on Stack Overflow See other posts from Stack Overflow or by jul
Published on 2010-03-15T18:02:23Z Indexed on 2010/03/15 19:19 UTC
Read the original article Hit count: 228

Filed under:
|
|
|
|

hi,

in a ModelForm I replaced a field by excluding it and adding a new one with the same name, as shown below in AddRestaurantForm. When saving the form with the code shown below, I get an error in form.save_m2m() ("Truncated incorrect DOUBLE value"), which seems to be due to the function to attempt to save the tag field, while it is excluded.

Is the save_m2m() function supposed to save excluded fields?
Is there anything wrong in my code?

Thanks
Jul

(...)
new_restaurant = form.save(commit=False)
new_restaurant.city = city
new_restaurant.save()

tags =  form.cleaned_data['tag']
if(tags!=''): tags=tags.split(',')
for t in tags:
    tag, created = Tag.objects.get_or_create(name = t.strip())
    tag.save()
    new_restaurant.tag.add(tag)

new_restaurant.save()
form.save_m2m()

models.py

class Tag(models.Model):
    name = models.CharField(max_length=100, unique=True)

class Restaurant(models.Model):
    name = models.CharField(max_length=50)
    city=models.ForeignKey(City)
    category=models.ManyToManyField(Category)
    tag=models.ManyToManyField(Tag, blank=True, null=True)

forms.py

class AddRestaurantForm(ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs=classtext))
    city = forms.CharField(widget=forms.TextInput(attrs=classtext), max_length=100)
    tag = forms.CharField(widget=forms.TextInput(attrs=classtext), required=False)

    class Meta:
        model = Restaurant
        exclude = ('city','tag') 

Traceback:
File "/var/lib/python-support/python2.5/django/core/handlers/base.py"
in get_response
92. response = callback(request, *callback_args,
**callback_kwargs)
File "/home/jul/atable/../atable/resto/views.py" in addRestaurant
498. form.save_m2m()
File "/var/lib/python-support/python2.5/django/forms/models.py" in
save_m2m
75. f.save_form_data(instance, cleaned_data[f.name])
File "/var/lib/python-support/python2.5/django/db/models/fields/
related.py" in save_form_data
967. setattr(instance, self.attname, data)
File "/var/lib/python-support/python2.5/django/db/models/fields/
related.py" in set
627. manager.add(*value)
File "/var/lib/python-support/python2.5/django/db/models/fields/
related.py" in add
430. self._add_items(self.source_col_name,
self.target_col_name, *objs)
File "/var/lib/python-support/python2.5/django/db/models/fields/
related.py" in _add_items
497. [self._pk_val] + list(new_ids))
File "/var/lib/python-support/python2.5/django/db/backends/util.py" in
execute
19. return self.cursor.execute(sql, params)
File "/var/lib/python-support/python2.5/django/db/backends/mysql/
base.py" in execute
84. return self.cursor.execute(query, args)
File "/var/lib/python-support/python2.5/MySQLdb/cursors.py" in execute
168. if not self._defer_warnings: self._warning_check()
File "/var/lib/python-support/python2.5/MySQLdb/cursors.py" in
_warning_check
82. warn(w[-1], self.Warning, 3)
File "/usr/lib/python2.5/warnings.py" in warn
62. globals)
File "/usr/lib/python2.5/warnings.py" in warn_explicit
102. raise message

Exception Type: Warning at /restaurant/add/
Exception Value: Truncated incorrect DOUBLE value: 'a'

© Stack Overflow or respective owner

Related posts about django

Related posts about model