Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 1.11-get_或_create中的奇怪行为_Django - Fatal编程技术网

Django 1.11-get_或_create中的奇怪行为

Django 1.11-get_或_create中的奇怪行为,django,Django,我在get\u或\u create 我没有与slug='ian-osborn' >>> DjProfile.objects.get(slug='ian-osborn') Traceback (most recent call last): File "/app/.heroku/python/lib/python3.6/code.py", line 91, in runcode exec(code, self.locals) File "<console&g

我在
get\u或\u create

我没有与slug='ian-osborn'

>>> DjProfile.objects.get(slug='ian-osborn')
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 380, in get
    self.model._meta.object_name
frontend.models.DoesNotExist: DjProfile matching query does not exist.
DjProfile模型定义如下:

class DjProfile(DateTimeModel):
    name = models.CharField(max_length=150, db_index=True)
    slug = models.SlugField(max_length=50, db_index=True, unique=True)
    ...

    class Meta:
        verbose_name = _('Dj Profile')
        verbose_name_plural = _('Dj Profiles')
        ordering = ('name', )
        unique_together = ('name', 'slug')

    def __str__(self):
        return self.name
我正在Django命令中运行类似的查询

from django.core.management.base import BaseCommand
from frontend.models import DjProfile

class Command(BaseCommand):
  ...

我做错了什么?

由于
name
字段是必需的,您应该将其作为
get\u或\u create
的参数,您是否成功创建了任何slug?对象创建似乎没有生成/保存slug,因此它尝试保存一个已经存在的空slug。是的,脚本成功创建了许多(数千)条记录。然后它开始随机失败。我在问题中添加了模型定义,
slug
字段是否按原样保存到数据库中,或者是否有一些自动过程来生成/更改它?由于
name
字段是必需的,我认为您应该将其作为
get\u或\u create
的参数。如果您想创建一个答案,我会将其标记为正确答案
from django.core.management.base import BaseCommand
from frontend.models import DjProfile

class Command(BaseCommand):
  ...