Django模型无效的元语法

时间:2015-10-14 05:46:30

标签: python django

在我的模型类中,我正在添加元类以获取有关细节的详细信息但是在运行之后 python manage.py makemigrations它显示错误 我的模型类是

来自django.db导入模型

# Create your models here.
class Category(models.Model):
    """Category model."""
    title = models.CharField( max_length=100)
    slug = models.SlugField(_(unique=True)

    class Meta:
        verbose_name = 'category'
        verbose_name_plural = 'categories'
        db_table = 'blog_categories'
        ordering = ('title',)

    def __str__(self):
        return u'%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('blog_category_detail', None, {'slug': self.slug})


class Post(models.Model):
    """Post model."""
    STATUS_CHOICES = (
        (1, 'Draft'),
        (2, 'Public'),
    )
    title = models.CharField( max_length=200)
    slug = models.SlugField(unique_for_date='publish')
    author = models.ForeignKey(User, blank=True, null=True)
    body = models.TextField( )
    tease = models.TextField(blank=True, help_text=_('Concise text suggested. Does not appear in RSS feed.'))
    status = models.IntegerField( choices=STATUS_CHOICES, default=2)
    allow_comments = models.BooleanField( default=True)
    publish = models.DateTimeField( default=datetime.datetime.now)
    created = models.DateTimeField( auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    categories = models.ManyToManyField(Category, blank=True)
    tags = TagField()
    objects = PublicManager()

    class Meta:
        verbose_name = 'post'
        verbose_name_plural ='posts'
        db_table  = 'blog_posts'
        ordering  = ('-publish',)
        get_latest_by = 'publish'

    def __str__(self):
        return u'%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('blog_detail', None, {
            'year': self.publish.year,
            'month': self.publish.strftime('%b').lower(),
            'day': self.publish.day,
            'slug': self.slug
        })

    def get_previous_post(self):
        return self.get_previous_by_publish(status__gte=2)

    def get_next_post(self):
        return self.get_next_by_publish(status__gte=2)


class BlogRoll(models.Model):
    """Other blogs you follow."""
    name = models.CharField(max_length=100)
    url = models.URLField()
    sort_order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ('sort_order', 'name',)
        verbose_name = 'blog roll'
        verbose_name_plural = 'blog roll'

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return self.url

并且出现错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/site-packages/django/core/management/__init__.py", line 325, in execute
    django.setup()
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1467, in exec_module
  File "<frozen importlib._bootstrap>", line 1572, in get_code
  File "<frozen importlib._bootstrap>", line 1532, in source_to_code
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/vivekpradhan/finalbasket/basket/blog/blogger/models.py", line 9
    class Meta:
        ^
SyntaxError: invalid syntax
(basket)vivekpradhan@vivekpradhan-AOD270:~/finalbasket/basket/blog$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/site-packages/django/core/management/__init__.py", line 325, in execute
    django.setup()
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/vivekpradhan/finalbasket/basket/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1467, in exec_module
  File "<frozen importlib._bootstrap>", line 1572, in get_code
  File "<frozen importlib._bootstrap>", line 1532, in source_to_code
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/vivekpradhan/finalbasket/basket/blog/blogger/models.py", line 9
    class Meta:
        ^
SyntaxError: invalid syntax

2 个答案:

答案 0 :(得分:0)

you lost a ")" at slug = models.SlugField(_(unique=True)

答案 1 :(得分:0)

在你提出这样的问题之前,你应该对你的代码进行拼写。