我在删除用户时遇到问题(仅用于测试),我不确定发生了什么。我正在运行python 3.6和Django 1.11.6。我的测试不使用数据库迁移,它们直接从模型创建数据库,但当我尝试从正常,正确迁移的数据库中删除现有条目时,也会发生同样的情况。
首先我创建一个用户:
user = User.objects.create_user('testing', 'testing@test.com', 'testpass')
我只是打电话:
user.delete()
它在delete()方法中崩溃了。用户甚至不需要添加相关的UserImage,它仍然会崩溃。最初我认为它是由相关对象引起的,因为例外情况说“不允许加入'加'',而'添加'是UserImage上的字段。任何帮助都是值得赞赏的,因为我看了几个小时,找不到原因:(
models.py
class UserManager(BaseUserManager):
def _create_user(self, username, email, password, **extra_fields):
if not username:
raise ValueError('Username must be set.')
email = self.normalize_email(email)
user = self.model(username=username, email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, username, email=None, password=None, **extra_fields):
return self._create_user(username, email, password, **extra_fields)
class User(AbstractBaseUser):
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
USERNAME_ALLOWED_REGEX = r'\w._-'
objects = UserManager()
username = models.CharField(
db_index=True,
max_length=10, unique=True,
help_text=_('10 characters or fewer. Letters, digits and ._- only.'),
validators=[
validators.RegexValidator(r'^[' + USERNAME_ALLOWED_REGEX + r']+$', _('Enter a valid username.'), 'invalid')
],
)
first_name = models.CharField(
max_length=30, blank=True,
help_text=_('Maximum 30 characters.'),
)
last_name = models.CharField(
max_length=30, blank=True,
help_text=_('Maximum 30 characters.'),
)
email = models.EmailField(
unique=True,
)
is_active = models.BooleanField(
default=True,
)
joined = models.DateTimeField(
auto_now_add=True, editable=False,
)
avatar = models.OneToOneField(
'UserImage',
null=True, blank=True, related_name='avatar_user',
on_delete=models.SET_NULL,
)
def add_image(self, name, content, save=True):
new_image = UserImage(user=self)
new_image.save_images(name, content, save=save)
return new_image
class UserImage(models.Model):
class Meta:
verbose_name = _('user_image')
verbose_name_plural = _('user_images')
ordering = 'id',
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
related_name='images',
on_delete=models.CASCADE,
)
added = models.DateTimeField(
auto_now_add=True, editable=False,
)
image = RenamedProcessedImageField(
upload_to=user_image_path,
processors=(ResizeToFit(2048, 2048, upscale=False),),
format='JPEG',
options={'quality': 100},
)
def save_images(self, name, content, save=True):
self.image.save(name, content, save=False)
if save:
self.full_clean(validate_unique=False)
self.save()
例外:
Traceback (most recent call last):
File "***/users/tests.py", line 237, in test_files_on_delete_rule
user.delete()
File "***/python3.6/site-packages/django/db/models/base.py", line 973, in delete
collector.collect([self], keep_parents=keep_parents)
File "***/python3.6/site-packages/django/db/models/deletion.py", line 222, in collect
elif sub_objs:
File "***/python3.6/site-packages/django/db/models/query.py", line 254, in __bool__
self._fetch_all()
File "***/python3.6/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "***/python3.6/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "***/python3.6/site-packages/django/db/models/sql/compiler.py", line 871, in execute_sql
sql, params = self.as_sql()
File "***/python3.6/site-packages/django/db/models/sql/compiler.py", line 423, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "***/python3.6/site-packages/django/db/models/sql/compiler.py", line 47, in pre_sql_setup
order_by = self.get_order_by()
File "***/python3.6/site-packages/django/db/models/sql/compiler.py", line 298, in get_order_by
field, self.query.get_meta(), default_order=asc))
File "***/python3.6/site-packages/django/db/models/sql/compiler.py", line 601, in find_ordering_name
field, targets, alias, joins, path, opts = self._setup_joins(pieces, opts, alias)
File "***/python3.6/site-packages/django/db/models/sql/compiler.py", line 634, in _setup_joins
pieces, opts, alias)
File "***/python3.6/site-packages/django/db/models/sql/query.py", line 1417, in setup_joins
names, opts, allow_many, fail_on_missing=True)
File "***/python3.6/site-packages/django/db/models/sql/query.py", line 1385, in names_to_path
" not permitted." % (names[pos + 1], name))
django.core.exceptions.FieldError: Cannot resolve keyword 'max' into field. Join on 'added' not permitted.
答案 0 :(得分:0)
好的,所以我发现问题实际上是由我的Topic模型上的排序定义引起的:
class ForumTopic(models.Model):
class Meta:
ordering = '-forum_posts__added__max',
当我删除它或将其更改为:
时,它可以正常工作class ForumTopic(models.Model):
class Meta:
ordering = '-id',