Answer the question
In order to leave comments, you need to log in
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
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)
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)
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question