Django 1.3.1 - 线程中未处理的异常

时间:2013-06-10 17:19:16

标签: django-1.3

我正在从一些视频教程中做django。我很难找出强制跟随错误的线。

Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0xb6e5722c>>
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 88, in inner_run
self.validate(display_num_errors=True)
File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 253, in validate
raise CommandError("One or more models did not validate:\n%s" % error_text)
django.core.management.base.CommandError: One or more models did not validate:
events.event: 'attendees' specifies an m2m relation through model Attendence, which has not been installed

我在bitbucket添加了源代码。

1 个答案:

答案 0 :(得分:0)

models.ManyToManyField()的Event类中,我在through上添加了错误的名称,这导致了问题。后来我用正确的类名解决了问题并解决了问题。

更正错误后,model.py看起来像

...
class Event(models.Model): ##error
    #"""docstring for Event"""
    description = models.TextField()
    creation_date = models.DateTimeField(default = datetime.now) #if you put the parenthesis on datetime.now every time changing the date called new function with different date and time
    start_date = models.DateTimeField(null = True,blank = True)
    creator = models.ForeignKey(User,related_name='event_creator_set')
    attendees = models.ManyToManyField(User,through='Attendance') #error occurs due to wrong class name on through
    latest = models.BooleanField(default = True)

    objects = EventManager()
    def __unicode__(self):
        return self.description

    def save(self,**kwargs):
        Event.objects.today().filter(latest=True,
            creator=self.creator).update(latest=False)
        super(Event,self).save(**kwargs)

class  Attendance(models.Model): ##error
    user = models.ForeignKey(User)#,related_name='attendance_user_set')
    event = models.ForeignKey(Event)#,related_name='attendance_event_set') ###error
    registration_date = models.DateTimeField(default=datetime.now)

    def __unicode__(self):
        return "%s is attending %s" %(self.user.username,self.event)
...

同样Django syncdb error: One or more models did not validate建议我添加related_name但排除它不会导致任何错误,所以我没有包含这个。