所以我有一个普通的DJango项目,有一些附加功能:
所以它一直在努力,直到我开始创建自定义"检索" Rest框架视图集中的请求。为了提供帮助,回溯如下:
Traceback (most recent call last):
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 195, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 39, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 13, in <module>
from django.core.servers.basehttp import get_internal_wsgi_application, run
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 17, in <module>
from django.core.handlers.wsgi import ISO_8859_1, UTF_8
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 10, in <module>
from django import http
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/http/__init__.py", line 5, in <module>
from django.http.response import (
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/http/response.py", line 13, in <module>
from django.core.serializers.json import DjangoJSONEncoder
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 23, in <module>
from django.core.serializers.base import SerializerDoesNotExist
File "/Users/Mason/Documents/dev/DIRNAME/SERCETDIR/env/lib/python2.7/site-packages/django/core/serializers/base.py", line 4, in <module>
from django.db import models
File "/Users/Mason/Documents/dev/DIRNAME/SECRETDIR/env/lib/python2.7/site-packages/django/db/models/__init__.py", line 3, in <module>
from django.db.models import signals # NOQA
ImportError: cannot import name signals
那就是说,我隐藏了一些个人信息,但如果您需要更多信息,我可以添加它。
我正在使用一个信号,在项目的某一点,但我没有做任何事情来打破它,根据pip,Django是V1.9。
对于您的信息,我只为整个项目使用一个models.py
,以避免混淆。
以下是我唯一models.py
的摘要:
`
from __future__ import unicode_literals
import string
import uuid
from datetime import timedelta, datetime
from random import randint
from django.contrib.auth.models import User, Group
from django.db import models
from django.utils.crypto import random
from django.utils.html import format_html
from helpers import fields # Doesn't import this file
class CardHolder(models.Model):
user = models.ForeignKey(User, related_name='rest_framework')
card_number = fields.CardNumberField(unique=True, max_length=16) # My special field
authentication_code = models.IntegerField()
transaction_auth_code = models.BigIntegerField()
universal_user_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
client_sign_up_key = models.CharField(max_length=20, default="hi")
def generate_attr(self):
self.authentication_code = randint(0, 2147483647) # 2147483647 is IntegerFields top limit
self.transaction_auth_code = randint(0, 9223372036854775807)
self.card_number = generate_card_number(max_length=16, min_length=16)
def __str__(self):
return self.user.username
def client(self):
try:
if len(self.user.groups.all()) > 0:
user_group = self.user.groups.all().last()
print "Here: ", self.user, ' ', user_group
client = Client.objects.get(name=user_group.name)
print client
universal_client_id = client.universal_client_id
client_url = "/admin/{}/client/{}/change".format(self._meta.app_label, universal_client_id)
return format_html("<span><a href='{}'>{}</a></span>".format(client_url, client.name))
else:
return "No Clients :("
except Exception as e:
raise e
def generate_client_sign_up_key(self):
size = 20
chars = string.ascii_uppercase + string.digits
code = ''.join(random.choice(chars) for _ in range(size))
self.client_sign_up_key = code
self.save()
return code
client_image_choices = (
('SQ', 'Square'),
('RC', 'Rectangle'),
('RO', 'Round')
)
class Client(models.Model):
temp = False
name = models.CharField(max_length=500, default="NAME")
email = models.EmailField(default="sam@themasons.org")
colour_style = models.CharField(default="ff0000", max_length=8, help_text="Please remove any # from the front")
icon = models.FileField(default="logo.png", help_text="If you can, please upload a square/round icon, not a big "
"rectangular shape.")
image_shape = models.CharField(max_length=20, choices=client_image_choices)
website = models.URLField(max_length=500, default="https://example.com/")
request_url_segment = models.CharField(max_length=500, default="api/request")
login_url_segment = models.CharField(max_length=500, default="api/login")
universal_client_id = models.UUIDField(primary_key=True, editable=True, default=uuid.uuid4)
description = models.CharField(max_length=500, default="HI")
admin = models.ForeignKey(User, default=1)
active = models.BooleanField(default=True)
deleted = models.BooleanField(default=False)
deletion_key = models.CharField(max_length=20)
def create_random_key(self, term_length, do_return_object):
random_key = randint(0, 9999999999999999)
date_time_created = datetime.now
random_key_object = ClientRandomKey(client=self, random_key=random_key, date_time_created=date_time_created,
valid_term_length=term_length)
random_key_object.save()
if do_return_object:
return random_key_object
def is_active(self):
return self.active
def delete(self, using=None, keep_parents=False):
group = Group.objects.get(name=self.name)
group.delete()
super(Client, self).delete()
def __str__(self):
return "{}".format(self.name)
def transactions_for_client(self):
transactions_for_client = TransactionLogItem.objects.filter(client=self)
return transactions_for_client
def generate_deletion_key(self):
size = 20
chars = string.ascii_uppercase + string.digits
code = ''.join(random.choice(chars) for _ in range(size))
self.deletion_key = code
self.save()
return code
class TransactionLogItem(models.Model):
expiry_length = 100 # In days
client = models.ForeignKey(Client)
card_holder = models.ForeignKey(CardHolder)
name = models.CharField(max_length=500, default="A transaction")
date_created = models.DateTimeField()
visible = models.BooleanField(default=True)
def make_invisible(self):
print "here"
self.visible = False
self.save()
def get_client(self):
return Client.objects.get(universal_client_id=self.client.universal_client_id)
def __str__(self):
return "Transaction for {}".format(self.card_holder)
class ClientRandomKey(models.Model):
client = models.ForeignKey(Client)
random_key = models.BigIntegerField()
date_time_created = models.DateTimeField(auto_now_add=True)
valid_term_length = models.IntegerField() # Number of days that the Key is valid for.
def get_finishing_date(self):
start_date_time = self.date_time_created
valid_term_length = self.valid_term_length
finish_date = start_date_time + timedelta(days=valid_term_length)
return finish_date
def is_valid(self):
if self.get_finishing_date() == datetime.now() or self.get_finishing_date() < datetime.now():
return False
else:
return True
def __str__(self):
return "Random Key for: {}".format(self.client)
def generate_card_number(**kwargs):
"""
Generates card number, used definitely in CardHolder.generate_attr()
:param kwargs:
:return:
"""
try:
max_length = kwargs["max_length"]
min_length = kwargs["min_length"]
value = []
if max_length >= min_length:
if max_length % 4 == 0:
segment_length = max_length / 4
for s in range(segment_length):
segment = random.randint(1000, 9999)
value.append(segment)
temp_value_str = map(str, value)
temp_value_parsed = "".join(temp_value_str)
return temp_value_parsed
else:
return None
else:
raise ValueError("min_length is greater than max_length")
except Exception as e:
raise e
`