开发者

Adding a model with a variable name in the main model

I have several file开发者_运维技巧s with names like: mod1.py, mod2.py, mod3.py, etc. They consist of unique Models and Fields.

mod1.py:

from django.db import models
class Report(models.Model):
    text = models.TextField()
    ...

models.py:

import random.random
id_part = random(1,10)
__import__('apps.reports.mod%s' % id_part)

When I try import it, the class is not found.

from models import Report

Is it possible to add another model in the model file with a variable name?


The problem is that your models aren't imported in the scope of models.py So you need to somehow implement from foo import * dynamically. Here you can find a solution (not very elegant).

Anyway it's very strange approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜