Вообщем такая вот ошибка, когда делаю migrate, - django.db.utils.IntegrityError: NOT NULL constraint failed: product_items.category_id. Уже гуглил, во всех решения что нашёл нужно поставить null=True во все модели, но это не помогло.
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, }),
]