matching query does not exist

I'm working on a Django project and I'm getting this error, "State matching query does not exist." why is this happening? the state is in the model store, can you explain me this error and how to solve it?

Thanks. Views:

def Test(request):
    stores = Store.objects.all()

    data = []

    for store in stores:
        data.append({
            'id': store.id,
            'name': store.name,
            'address': store.address,
            'state': store.state,
        })

    return HttpResponse(json.dumps(data), 'application/json')

Models:

class State(models.Model):
    name = models.CharField(max_length=255)
    shortname = models.CharField(max_length=100)


    def __unicode__(self):
        return self.name


class City(models.Model):
    name = models.CharField(max_length=255)
    state = models.ForeignKey(State)

    def __unicode__(self):
        return self.name


class Store(models.Model):
    name = models.CharField(max_length=255, null=True, blank=True)
    address = models.CharField(max_length=255, blank=True, null=True)
    lon = models.FloatField(blank=True, null=True)
    lat = models.FloatField(blank=True, null=True)
    state = models.ForeignKey(State)
    city = models.ForeignKey(City)


    def __unicode__(self):
        return self.name

Traceback:

Environment:

Request Method: GET Request URL: http://127.oo1:8000/posts/Test/

Django Version: 1.7.1 Python Version: 2.7.6 Installed Applications: ('django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'clients', 'cobrands', 'core', 'posts', 'bootstrapform', 'rest_framework') Installed Middleware: ('django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback: File "/home/tabzz/c-dev/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/tabzz/c/posts/views.py" in Test 34. 'state': store.state, File "/home/tabzz/c-dev/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in get 572. rel_obj = qs.get() File "/home/tabzz/c-dev/local/lib/python2.7/site-packages/django/db/models/query.py" in get 357. self.model._meta.object_name)

Exception Type: DoesNotExist at /posts/Test/ Exception Value: State matching query does not exist.


It looks like you have a Store model in your database that does not have a state . When you try to access state here:

data.append({
    'id': store.id,
    'name': store.name,
    'address': store.address,
    'state': store.state,
})

Django throws an error because state is a ForeignKey on Store and it has to look it up when you access it with store.state . If state is empty and you haven't set your ForeignKey to allow None values with null=True , Django will raise a DoesNotExist exception.

链接地址: http://www.djcxy.com/p/55910.html

上一篇: 升级Django和NoReverseMatch密码错误

下一篇: 匹配查询不存在