简体   繁体   中英

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

After I makemigrations, I am getting this error when I try to migrate. mathfield is installed in INSTALLED_APPS.

from django.db import models
from django.utils import timezone
import mathfield

class Post(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length=200)
    text = models.TextField()
    latex = mathfield.MathField(default=timezone.now)
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

django-mathfield is installed in my virtualenv django-mathfield usage The error happens with the default or if I allow the one-off default during makemigrations.

This package is for Django 1.7. If you are using a newer version of Django, the package may have failing imports. If this is the case, you can revert to an earlier version of Django or you can modify the django-mathfield package to match your version of Django.

Edit: I looked at the library code, and it seems the imports in __init__.py are causing issues. I was able to get it to work by removing the top imports from mathfield/__init__.py :

# from mathfield.api import *
# from mathfield.models import *
# from mathfield.widgets import *

And changing your code as follows:

from mathfield.models import MathField

class Post(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length=200)
    text = models.TextField()
    latex = MathField(default=timezone.now)
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(
    blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title
$ pip install django-mathfield

先安装数学字段然后再做

import mathfield

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM