Django从models.py导入类 [英] Django Import Class From models.py

查看:28
本文介绍了Django从models.py导入类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这样的文件夹结构:

Using a folder structure like this:

library/
-django.wsgi
-manage.py
-static/
    --all my static files
-library/
    --__init__.py
    --models.py
    --settings.py
    --urls.py
    --views.py
    --wsgi.py
    --templates/
        ---where i plan to store all my templates

如何在我的 views.py 中导入在 models.py 中定义的类?

How can i import a class in my views.py that is defined in models.py?

我试过了:

from . import models.class

from models import class

from projectname.models import class

from projectname import models.class

from project import class

但是对于所有这些我都收到无效的语法错误

But for all those i get invalid syntax errors

views.py

from django.core.context_processors import csrf
from django.shortcuts import redirect, render
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.contrib import messages
from django.template import RequestContext, loader
from django.contrib.auth import logout

from library.models import 7DTagmap

models.py

from __future__ import unicode_literals

from django.db import models

class 7DTagmap(models.Model):
   id = models.IntegerField(primary_key=True)
    tag_id = models.CharField(max_length=50L)
    st_tag_id = models.IntegerField()
    class Meta:
        db_table = '7d_tagmap'

错误:

invalid syntax (views.py, line 11)
Exception Type: SyntaxError
Exception Value:    invalid syntax (views.py, line 11)

推荐答案

use:

from library.models import MyClass

你应该很高兴:)

(基本结构是from .models import )

更新:

问题是(几乎!)当然,您的模型以7"开头——将其更改为字母字符,一切都会好起来的,我(几乎!)确定:)

the problem is (almost!) certainly that your model begins with '7' -- change it to a letter character, and all will be well, I'm (almost!) sure :)

这篇关于Django从models.py导入类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆