Django ImportError: unable to import name 'x'

I have problems with roundness in two files. The import function of the model to run when creating the object and the import model of this function to verify the uniqueness of the code.

How to use the model in functions and functions in the model without problems with roundness? I checked simillar questions to my problem, but I still don't know to fix this problem.

models.py

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import models
from .middleware.current_user import get_current_user
from shortener.utils import create_shortcode
from django.conf import settings
CODE_MAX_LENGTH = getattr(settings, 'CODE_MAX_LENGTH', 16)


class Shortener(models.Model):
    url = models.URLField()
    code = models.CharField(unique=True, blank=True, max_length=CODE_MAX_LENGTH)
    author = models.ForeignKey(User, blank=True, null=True)  # Allow anonymous
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    active = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        if not self.pk:
            self.author = get_current_user()

            if self.code in [None, ""]:
                self.code = create_shortcode()
            elif self.code.find(' ') != -1:
                self.code = self.code.replace(' ', '_')

            if self.url not in ["http", "https"]:
                self.url = "http://{0}".format(self.url)

        super(Shortener, self).save(*args, **kwargs)

    def __str__(self):
        return self.url

    def __unicode__(self):
        return self.url

    def get_short_url(self):
        return reverse("redirect", kwargs={'code': self.code})

Utils.py

import random
import string
from django.conf import settings
from shortener.models import Shortener
SIZE = getattr(settings, 'CODE_GENERATOR_MAX_SIZE', 12)


def code_generator(size=SIZE):
    return ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(size))


def create_shortcode():
    code = code_generator()

    if Shortener.objects.filter(code=code).exists():
        create_shortcode()

    return code

Traceback:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x037EAB28>
Traceback (most recent call last):
  File "C:\Users\loc\shortener\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\loc\shortener\lib\site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\loc\shortener\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "C:\Users\loc\shortener\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\loc\shortener\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\loc\shortener\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\loc\shortener\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:\Users\loc\shortener\lib\site-packages\django\apps\config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "E:\Python\Python35-32\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "C:\Users\loc\PycharmProjects\DjangoURLShortener\shortener\models.py", line 4, in <module>
    from shortener.utils import create_shortcode
  File "C:\Users\loc\PycharmProjects\DjangoURLShortener\shortener\utils.py", line 4, in <module>
    from shortener.models import Shortener
ImportError: cannot import name 'Shortener'
+4
source share
1 answer

: create_shortcode models.py, 3 , . Shortener.save self.objects.filter(...).

:. uuid uuid.uuid4 ( ) . 12- 12- , UUID 16- . , :

code = models.CharField(unique=True, max_length=16, default=uuid.uuid4)
+2

Source: https://habr.com/ru/post/1664831/


All Articles