Hi, i'm new on Django, and i'm getting a validation error that i don't
undestand.
Here my models:
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
# Create your models here.
class Categoria(models.Model):
nom = models.CharField(max_length = 100)
imatge = models.ImageField(upload_to = 'img/noticies/categories/')
def __unicode__(self):
return self.nom
class Meta():
verbose_name = u'categoria'
verbose_name_plural = u'categories'
class Noticia(models.Model):
usuari = models.ForeignKey(User, related_name =
'usuarinoticia_set')
categoria = models.ForeignKey(Categoria, related_name =
'adminnoticia_set')
admin = models.ForeignKey(User, blank = True, null = True)
titol = models.CharField(max_length = 255)
url = models.CharField(max_length = 255, unique = True)
resum = models.TextField('resum de la notícia', blank = True, null
= True)
contingut = models.TextField('contingut de la notícia')
aprovada = models.NullBooleanField(blank = True, null = True)
creada_el = models.DateTimeField(editable = False)
def __unicode__(self):
return self.titol
def save(self):
if not self.id:
self.creada_el = datetime.now()
else:
if self.aprovada == False:
# Eliminar les imatges, comentaris
self.imatges.all().delete()
self.comentaris.all().delete()
super(Noticia, self).save()
class Meta():
verbose_name = u'notícia'
verbose_name_plural = u'notícies'
class Imatge(models.Model):
noticia = models.ForeignKey(Noticia, related_name = 'imatges')
imatge = models.ImageField(upload_to='img/noticies/')
def __unicode__(self):
return self.imatge.__str__()
class Meta():
verbose_name = u'imatge'
verbose_name_plural = u'imatges'
class Comentari(models.Model):
noticia = models.ForeignKey(Noticia, related_name = 'comentaris')
usuari = models.ForeignKey(User)
titol = models.CharField(max_length = 100)
contingut = models.TextField()
creat_el = models.DateTimeField(editable = False)
def save(self):
if not self.id:
self.creat_el = datetime.now()
super(Comentari,self).save()
def __unicode__(self):
return self.titol
class Meta():
verbose_name = u'comentari'
verbose_name_plural = u'comentaris'
As you can see, when a Noticia object is set to aprovada = False, it
tries to erase the related objects.
When there not exist any related object, there is no problem, but when
exist a related object (imatges or comentaris) i get this validation
error:
Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/
base.py" in get_response
100. response = callback(request,
*callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/
options.py" in wrapper
239. return self.admin_site.admin_view(view)(*args,
**kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/
decorators.py" in _wrapped_view
76. response = view_func(request, *args,
**kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/
cache.py" in _wrapped_view_func
69. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/
sites.py" in inner
190. return view(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/
decorators.py" in _wrapper
21. return decorator(bound_func)(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/
decorators.py" in _wrapped_view
76. response = view_func(request, *args,
**kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/
decorators.py" in bound_func
17. return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.6/dist-packages/django/db/transaction.py"
in _commit_on_success
299. res = func(*args, **kw)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/
options.py" in change_view
898. self.save_formset(request, form, formset,
change=True)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/
options.py" in save_formset
603. formset.save()
File "/usr/local/lib/python2.6/dist-packages/django/forms/models.py"
in save
487. return self.save_existing_objects(commit) +
self.save_new_objects(commit)
File "/usr/local/lib/python2.6/dist-packages/django/forms/models.py"
in save_existing_objects
595. pk_value = form.fields[pk_name].clean(raw_pk_value)
File "/usr/local/lib/python2.6/dist-packages/django/forms/fields.py"
in clean
162. value = self.to_python(value)
File "/usr/local/lib/python2.6/dist-packages/django/forms/models.py"
in to_python
991. raise
ValidationError(self.error_messages['invalid_choice'])
I've loocked the code in /usr/local/lib/python2.6/dist-packages/django/
forms/models.py, and the exception is raised in the class
ModelChoiceField(ChoiceField), and here is were i'm lost, since my
models doesn't have any ModelChoiceField...
If somebody can point me what i'm doing wrong i'll thankful.
I'm using django 1.2.1 on ubuntu 10.4.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.