未知命令'refreshcodes'

时间:2016-11-03 15:27:21

标签: python django command django-admin manage.py

我是python中的新手,并关注youtube视频以了解如何编码。但现在我遇到了问题。我试着写自定义django-admin命令'refreshcodes'。这是我的文件位于src / shortener / managment / commands / refreshcodes.py:

from django.core.management.base import BaseCommand, CommandError

from shortener.models import KirrURL

class Command(BaseCommand):
    help = 'Refreshes all KirrURL shortcodes'

    def handle(self, *args, **options):
        return KirrURL.objects.refresh_shortcodes() 

这是shortener / models.py:

import random
import string
from django.db import models
from .utils import code_generator, create_shortcode
# Create your models here.


class KirrURLManager(models.Manager):
    def all(self, *args, **kwargs):
        qs_main = super(KirrURLManager, self).all(*args, **kwargs)
        qs = qs_main.filter(active=True)
        return qs
    def refresh_shortcodes(self):
        qs = KirrURL.objects.filter(id__gte=1)
        new_codes = 0
        for q in qs:
            q.shortcode = create_shortcode(q)
            print (q.shortcode)
            q.save()
            new_codes += 1
        return "New codes made: {i}".format(i=new_codes)



class KirrURL(models.Model):
    url         = models.CharField(max_length=220, )
    shortcode   = models.CharField(max_length=15, unique=True, blank=True)
    updated     = models.DateTimeField(auto_now=True) #everytime model is saved
    timestamp   = models.DateTimeField(auto_now_add=True) #when model was created
    active      = models.BooleanField(default=True)
    #empty_datetime = models.DateTimeField(auto_now=False, auto_now_add=False)
    #shortcode = models.CharField(max_length=15, null=True) Empty in database is ok
    #shortcode = models.CharField(max_length=15, default='cfedefaultshortcode')

    objects = KirrURLManager()

    #some_random = KirrURLManager()

    def save(self, *args, **kwargs):
        if self.shortcode is None or self.shortcode == "":
            self.shortcode = create_shortcode(self)
        super(KirrURL, self).save(*args, **kwargs)
    #def my_save(self):
    #   self.save()
    def __str__(self):
        return str(self.url)

所以,当我试图运行python manage.py刷新代码时,我遇到了一个错误:

未知命令:'refreshcodes' 输入'manage.py help'进行使用。

然后我注意到在refreshcodes.py中shortener.models被强调,所以我尝试使用Pycharm帮助菜单修复它。 Pycharm错误:

Collecting models
  Using cached models-0.9.3.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/3d/xgytpbqn7k113z6fx_jxdq9r0000gn/T/pycharm-packaging0.tmp/models/setup.py", line 25, in <module>
        import models
      File "/private/var/folders/3d/xgytpbqn7k113z6fx_jxdq9r0000gn/T/pycharm-packaging0.tmp/models/models/__init__.py", line 23, in <module>
        from base import *
    ImportError: No module named 'base'

    ----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/3d/xgytpbqn7k113z6fx_jxdq9r0000gn/T/pycharm-packaging0.tmp/models/

请帮帮我!我正在使用django 1.10.2和python 3.5.2。

0 个答案:

没有答案