0

我目前正在从一些视频教程中做 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包含源代码。

4

1 回答 1

0

models.ManyToManyField()事件类中,我输入了错误的名称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 但排除它不会导致任何错误,所以我没有包括这个。

于 2013-06-11T14:54:25.820 回答