2017-06-01 82 views
1

我是新來的Django,我試圖通過一些項目實踐學習,但我堅持這個問題,失敗的Django NOT NULL約束:shop_product.user_id

NOT NULL約束失敗:shop_product.user_id

models.py

from django.db import models 
from django.conf import settings 
from django.core.urlresolvers import reverse 
class Category(models.Model): 
    user = models.ForeignKey(settings.AUTH_USER_MODEL, 
          related_name='category_created') 
    name = models.CharField(max_length=200, db_index=True) 
    slug = models.SlugField(max_length=200, db_index=True, unique=True) 

    class Meta: 
     ordering = ('name',) 
     verbose_name = 'category' 
     verbose_name_plural = 'categories' 

    def __str__(self): 
     return self.name 

class Product(models.Model): 
    user = models.ForeignKey(settings.AUTH_USER_MODEL, 
          related_name='product_created') 
    category = models.ForeignKey(Category, related_name='products') 
    name = models.CharField(max_length=200, db_index=True) 
    slug = models.SlugField(max_length=200, db_index=True) 
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True) 
    description = models.TextField(blank=True) 
    price = models.DecimalField(max_digits=10, decimal_places=2) 
    stock = models.PositiveIntegerField() 
    available = models.BooleanField(default=True) 
    negiotiable = models.BooleanField(default=True) 
    created = models.DateTimeField(auto_now_add=True) 
    updated = models.DateTimeField(auto_now=True) 
    users_like = models.ManyToManyField(settings.AUTH_USER_MODEL, 
             related_name='product_liked', 
             blank=True) 

    class Meta: 
     ordering = ('name',) 
     index_together = (('id', 'slug'),) 

    def __str__(self): 
     return self.name 
    def get_absolute_url(self): 
     return reverse('products_detail', 
         args=[self.slug]) 

這裏是我的views.py但我不知道是否有問題我的意見,我認爲這個問題將不得不與我的模型....

views.py

from django.views.generic import * 
from django.core.urlresolvers import reverse_lazy 
from .models import Category, Product 

class CategoryList(ListView): 
    model = Category 

class CategoryDetail(DetailView): 
    model = Category 

class ProductList(ListView): 
    model = Product 

class ProductDetail(DetailView): 
    model = Product 

class ProductCreate(CreateView): 
    model = Product 
    fields = ("category", 'name', 'image', 'description', 'price', 'stock','available', 'negiotiable') 

class ProductUpdate(UpdateView): 
    model = Product 
    fields = ('name', 'image', 'description', 'price', 'stock','available', 'negiotiable') 

class ProductDelete(DeleteView): 
    model = Product 
    success_url = reverse_lazy('product_list') 

請讓我知道什麼可以做。

+0

你有「商店」模型嗎? – doru

+0

您的產品創建視圖不包括用戶。發生這種情況時您是否試圖創建產品? – Brobin

+0

你可以顯示網址嗎? – zaidfazil

回答

2

要麼添加用戶到列表:

class ProductCreate(CreateView): 
    model = Product 
    fields = ["category", 'name', 'image', 'description', 'price','stock','available', 'negiotiable', 'user'] 

,或者確保字段必須是空=真和空白=真。

class Product(models.Model): 
    user = models.ForeignKey(settings.AUTH_USER_MODEL, 
         related_name='product_created', null=True, blank=True) 
+0

我試過你的解決方案仍然無法正常工作,而且m仍然收到相同的錯誤信息。無論如何,謝謝 –

+0

當你改變你的領域時,你是否再次遷移它? – Sagar

+0

它的工作,但現在我得到noreversematch在/產品/創建 –

0

添加形式爲您的產品,

class ProductForm(forms.ModelForm): 

    class Meta: 
     model = Product 
     fields = "__all__" 

此外,您user字段必須null=Trueblank=True

在創建視圖

然後,

class ProductCreate(CreateView): 
    model = Product 
    form_class = ProductForm 

    def form_valid(self, form): 
     form.instance.user = self.request.user 
     form.save() 
     return super(ProductCreate, self).form_valid(form) 

現在,每當創建一個產品,誰創建的用戶將被添加爲產品的用戶。

相關問題