column slug并不是唯一的

时间:2014-02-08 12:53:12

标签: django python-2.7 django-forms slug

当我从我的表单添加数据并获得此错误时。 / open_shop /的IntegrityError column slug不是唯一的

但经过测试和slu it独显

class OpenShopForm(forms.ModelForm):
    class Meta:
        model = Shop


class OpenShop(CreateView):
    model = Shop
    form_class = OpenShopForm

def form_valid(self, form):
    f = form.save(commit=False)
    f.submitter = self.request.user
    if self.request.method == 'POST':
        f = OpenShopForm(self.request.POST)
    f.save()

    return super(CreateView, self).form_valid(form)

def get_success_url(self):
    return reverse("profile", kwargs={'slug': self.request.user})

如果我通过管理员添加相同内容,则可以正常使用

此代码不保存图片:

class OpenShop(CreateView):
    model = Shop
    form_class = OpenShopForm

    def form_valid(self, form):
        f = form.save(commit=False)
        f.submitter = self.request.user
        f.save()
        return super(CreateView, self).form_valid(form)

    def get_success_url(self):
        return reverse("profile", kwargs={'slug': self.request.user})

{% extends "base.html"%}
{% block content %}


   <form enctype="multipart/form-data" action="{% url 'addproduct' %}" method="post">{% csrf_token %}
        {{ form.non_field_errors }}
          <p>
            {{form.title.errors}}
            <label class="txtField" for="id_subject">Product name:</label>
            {{ form.title }}
        </p>
        <p>
            {{form.slug.errors}}
            <label class="txtField" for="id_subject">Slug:</label>
            {{ form.slug }}
        </p>
        <p>
            {{form.catalog.errors}}
            <label class="txtField" for="id_subject">Product catalog:</label>
            {{ form.catalog }}

        <p>
            {{form.subcatalog.errors}}
            <label class="txtField" for="id_subject">Product subcatalog:</label>
            {{ form.categories }}
        </p>

        <p>
            {{form.description.errors}}
            <label class="txtField" for="id_subject">Product descriptions:</label>
            {{ form.description }}
        </p>
        <p>
            {{form.price.errors}}
            <label class="txtField" for="id_subject">Product price:</label>
            {{ form.price }}
        </p>
        <p>
            {{form.sale_price.errors}}
            <label class="txtField" for="id_subject">Product sale price:</label>
            {{ form.sale_price }}
        </p>
        <p>
            {{form.is_active.errors}}
            <label class="txtField" for="id_subject">Product activity:</label>
            {{ form.is_active }}
        </p>
        <p>
            {{form.is_featured.errors}}
            <label class="txtField" for="id_subject">Product featured:</label>
            {{ form.is_featured }}
        </p>
        <p>
            {{form.quantity.errors}}
            <label class="txtField" for="id_subject">Product quantity:</label>
            {{ form.quantity }}
        </p>
        <p>
            {{form.shop.errors}}
            <label class="txtField" for="id_subject">Product shop:</label>
            {{ form.shop }}
        </p>


        <input type="submit" value="Submit" />
    </form>

{% endblock %

}

models.py

class Product(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    image = models.ImageField(verbose_name=u'Image', upload_to="product_pic", blank=True)
    catalog = models.ForeignKey(Catalog)
    subcatalog = models.ManyToManyField(Subcatalog)
    description = models.TextField(blank=True, help_text="Describe product")
    submitter = models.ForeignKey(User, related_name="product vendor")
    price = models.DecimalField(max_digits=15, decimal_places=2)
    sale_price = models.DecimalField(max_digits=15, decimal_places=2)
    is_active = models.BooleanField(default=True)
    is_featured = models.BooleanField(default=False)
    quantity = models.IntegerField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    shop = models.ForeignKey(Shop)


    def __unicode__(self):
        return self.title

3 个答案:

答案 0 :(得分:2)

我不知道你为什么在if self.request.method == 'POST'中开始form_valid这两行。只有在表单有效时才会调用该方法,只有在首先从POST实例化时才会发生这种情况。正如yuvi所说,在该声明中,您将使用基于POST的新表单替换现有表单,覆盖您之前为设置submitter所做的操作。

答案 1 :(得分:1)

不可否认,我从未使用过基于类的视图,文档也很模糊。但试试:

 def form_valid(self, form):
    form.instance.submitter = self.request.user
    return super(CreateView, self).form_valid(form)

答案 2 :(得分:1)

有效!

class AddProduct(CreateView):
        model = Product
        form_class = AddproductForm

        def form_valid(self, form):
            form.instance.submitter = self.request.user
            return super(AddProduct, self).form_valid(form)
        def get_success_url(self):
            return reverse("profile", kwargs={'slug': self.request.user})