【问题标题】:django model with IpAddressField throws error in syncdb带有 IpAddressField 的 django 模型在 syncdb 中引发错误
【发布时间】:2013-03-02 19:21:12
【问题描述】:

我有一个 models.py 文件,其中定义了以下类:

from django.db import models
from bands.models import Genre, Song

class Lyric(models.Model):
    """A music lyric"""
    class Meta:
        db_table = 'lyric'
    genre = models.ForeignKey(Genre)
    song = models.ForeignKey(Song,unique=True)
    lyric_text = models.TextField()
    created_at = models.DateTimeField()
    def __unicode__(self):
        return "\"" + self.title + "\" by " + self.author
    def lyric_short(self, length=100):
        return self.lyric_text[:length] + "..."

class LyricComment(models.Model):
    """Comment on a lyric"""
    class Meta:
        db_table = 'lyric_comment'
    lyric = models.ForeignKey(Lyric)
    text = models.TextField()
    author = models.CharField(max_length=64)
    ip = models.IpAddressField()
    created_at = models.DateTimeField()
    def __unicode__(self):
        return "\"" + self.text[:10] + "...\" by " + self.author
    def text_short(self, length=100):
        return self.text[:length] + "..."

运行时:

$ python manage.py syncdb

抛出以下错误:

AttributeError: 'module' object has no attribute 'IpAddressField'

ip = models.IpAddressField() 有什么问题?

【问题讨论】:

    标签: python django model ip field


    【解决方案1】:

    拼写为IPAddressField,大写P

    【讨论】:

    • 真可惜 ;) 正是——就是这样。
    【解决方案2】:

    这是拼写错误。

    ip = models.IpAddressField() 更改为ip = models.IPAddressField()

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 2014-09-07
      • 2011-02-06
      • 2010-12-09
      • 1970-01-01
      • 2014-09-21
      • 2021-10-17
      • 2012-05-09
      • 1970-01-01
      相关资源
      最近更新 更多