5
5
50VAJJ2017-03-17 14:36:59
Django
50VAJJ, 2017-03-17 14:36:59

Error django.db.utils.IntegrityError: NOT NULL constraint failed: product_items.category_id?

Generally such here an error when I do migrate, - django.db.utils.IntegrityError: NOT NULL constraint failed: product_items.category_id. Already googled, in all the solutions that I found, you need to put null = True in all models, but this did not help.
models.py

from django.db import models


class Category(models.Model):
    name = models.CharField(max_length=255, verbose_name='Название категории', default='', null=True)
    alias = models.SlugField(verbose_name='Alias категории', default='', null=True)

    class Meta:
        verbose_name = "Категория"
        verbose_name_plural = "Категории"

    def __str__(self):
        return 'Категория %s' % self.name


class Items(models.Model):
    name = models.CharField(max_length=15, verbose_name='Название товара',  null=True)
    price = models.IntegerField(default=0, verbose_name='Цена', null=True)
    # image = models.CharField(max_length=255, verbose_name='Картинка', default='')
    image = models.ImageField(null=True, blank=True, upload_to='image/', verbose_name='Изображение')
    alias = models.SlugField(verbose_name='Alias товара', null=True)

    category = models.ForeignKey(Category, default='',  null=True)

    class Meta:
        verbose_name = "Товар"
        verbose_name_plural = "Товары"

    def __str__(self):
        return 'Товар %s' % self.name

admin.py
from django.contrib import admin
from .models import *
from popular.models import *


class ItemAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'price', 'image')


class CategoryAdmin(admin.ModelAdmin):
    list_display = ('id', 'name')


admin.site.register(Items, ItemAdmin)
admin.site.register(Category, CategoryAdmin)

views.py
def products(request, alias):
    try:
        product = Items.objects.get(alias=alias)
        title = product.name
    except:
        raise Http404("Объект не найден")

    return render(request, 'product/product.html', locals())


def get_category(request, alias):
    try:
        category = Category.objects.get(alias=alias)
        product = Items.objects.filter(category=category)
    except:
        raise Http404('Объекты не найден')
    context = {
        'product': product,
        'category': category,
    }
    return render(request, 'product/product.html', context)

urls.py
url(r'^admin/', admin.site.urls),
    url(r'^$', views.popular, name='popular'),
    url(r'^products/(?P<alias>[^/]+)', views.products, name='product'),
    url(r'^get_category/(?P<alias>[^/]+)', views.products, name='product'),

]
if settings.DEBUG:
    urlpatterns += [
        url(r'^media/(?P<path>.*)$', serve,
            {'document_root': settings.MEDIA_ROOT, }),
    ]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
5
50VAJJ, 2017-03-17
@50VAJJ

I did everything thanks to this article - stackoverflow.com/questions/36053014/no-such-colum...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question