Andrej Vilenskij
Andrej Vilenskij

Reputation: 517

django: cant import models from global app

Trying to import models from different app in views.py:

from global.models import Global

Console raise an error:

    from global.models import Global
              ^
SyntaxError: invalid syntax

I checked a lot of information, but cant find what iam doing wrong.

global.models.py

from django.db import models

# Create your models here.
class Global(models.Model):
    phone = models.CharField(max_length=50, blank=True)
    email = models.EmailField(max_length=50, blank=True)
    adress = models.CharField(max_length=100, blank=True)

    class Meta:
        verbose_name = 'Глобальные настройки'
        verbose_name_plural = 'Глобальные настройки'

    def __str__(self):
        return 'Глобальные настройки'

projects.views.py

from django.shortcuts import render, redirect, get_object_or_404
from global.models import Global
from .models import Advertising, Films, Presentations, AuthorsLeft, AuthorsRight, BackstageImg
from django.utils import timezone

in my editor 'global' has blue color, and if i try to import another app it works normally. Could it be that 'global' reserved by django, or i simply doing something wrong?

Upvotes: 0

Views: 165

Answers (1)

A'zam Mamatmurodov
A'zam Mamatmurodov

Reputation: 370

from django.db.models.loading import get_model
global_model = get_model('global', 'Global')

You can use it, like this:

first_instance = global_model.objects.first()
print("Phone: {}".format(first_instance.phone))

Upvotes: 2

Related Questions