Django UpdateView创建一个新对象而不是更新当前对象

时间:2015-05-14 17:52:25

标签: django django-forms django-class-based-views

每次我使用UpdateView并尝试更改某些属性时,它会使用新的唯一主键在我的数据库中创建一个新对象。我的模型如下。

class Delivery(models.Model):
    created_date = models.DateTimeField('date created', editable=False)
    modified_date = models.DateTimeField('modified', editable=False)
    user_name = models.ForeignKey(User, null=False)
    stream_name = models.CharField(max_length=50, null=False)
    view_name = models.CharField(max_length=100, null=False, blank=True)
    activity_name = models.CharField(max_length=100, null=False, blank=True)
    jira = models.URLField()
    codereview = models.URLField()
    related_streams = models.CharField(max_length = 100, choices=sorted(streams()))
    description = models.TextField(null=False,blank=True)
    status = models.BooleanField(default=False, blank=False)

    class Meta:
        verbose_name = "Delivery"
        verbose_name_plural = "Deliveries"
        unique_together = (("user_name", "view_name", "activity_name"),)

对于我正在使用ModelFactoryForm的表单,UpdateView默认使用它来使用模型本身挑选一个form_class。

class UpdateEntryView(UpdateView):
    template_name = 'tracker/update.html'
    model = Delivery
    success_url = reverse_lazy('table_view')
    status = StreamStatus()
    fields = ['stream_name','view_name','activity_name','jira','related_streams','description','status']

    def get_context_data(self, **kwargs):
        ctx = super(UpdateEntryView, self).get_context_data(**kwargs)
        ctx['locked'] = self.status.getLocked()
        ctx['unlocked'] = self.status.getUnlocked()
        return ctx

HTML模板如下:

...
{% block content %}
<form action="/tracker/entry/" method="post" class="form">
    {% csrf_token %}
    <div class="form-goup">
        <div class="panel panel-default">
            <div class="panel-heading">Enter the codereview link and let the machine do the work for you ...</div>
            <div class="panel-body">
                <div class="input-group">
                    <i class="glyphicon glyphicon-link input-group-addon" aria-hidden="true"></i>
                    <input type="text" class="form-control" name="codereview"  placeholder="Codereview link">
                </div>
            </div>
        </div>
        <hr>
        <div class="panel panel-default">
            <div class="panel-heading">Add an entry manually ...</div>
            <div class="panel-body">
                {% bootstrap_form form %}
            </div>
        </div>
        <hr>
        {% buttons %}
        <button type="submit" class="btn btn-primary btn-block">
        {% bootstrap_icon "lock" %} Submit
        </button>
        {% endbuttons %}
    </div>
</form>
{% endblock %}
...

这是UpdateView中的一个缺陷还是我自己的一些事情搞砸了。

2 个答案:

答案 0 :(得分:3)

您的网址是否可能指向CreateView而不是UpdateView?

答案 1 :(得分:2)

模板中的行会将其发布到&#34; / tracker / entry /&#34; url,没有指定PK,我认为这是导致你问题的原因。

更新视图应在URL中包含PK,id或slug,因此Django知道要更新的对象。

删除网址,然后POST回到与GET相同的位置。

<form action="" method="post" class="form">